Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

James Tursa
James Tursa am 26 Nov. 2015
n = whatever;
x = zeros(n);
x(randperm(n*n,n)) = 1;

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by