I want to create a nxn matrix with all 0s and n randomly placed 1s

4 次查看(过去 30 天)
I want to create a nxn zero matrix with n randomly placed ones in the matrix
Example
1st try
1 0 0
0 1 0
0 0 1
2nd Try
0 0 1
0 1 1
0 0 0

采纳的回答

James Tursa
James Tursa 2015-11-26
n = whatever;
x = zeros(n);
x(randperm(n*n,n)) = 1;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by