how to make a matrix elements randomly distributed
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
how to make a matrix m elements, which has n elements "one" or #0, randomly distributed, the remaining elements are zero (n<m).
0 Kommentare
Akzeptierte Antwort
David Sanchez
am 21 Jun. 2013
Try this out (adapt the values to your needs):
m = 10;
n = 3;
pos = randperm(m*n,m);
M = zeros(m,n);
M(pos) = 1;
M =
0 0 0
1 1 0
0 1 1
0 0 0
1 0 0
0 0 0
0 0 0
1 0 1
0 1 1
0 0 1
M has the ones randomly distributed.
1 Kommentar
Jan
am 21 Jun. 2013
And for older Matlab versions:
M = zeros(m,n);
pos = randperm(m*n);
M(pos(1:n)) = 1;
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Contour Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!