How to distribute 100 ones in 100 by 100 matrix randomly in all posibilities
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Amit
am 10 Feb. 2012
Bearbeitet: Image Analyst
am 15 Okt. 2013
I want to make binary matrix, and distribute 100 ones in it, randomly in all possibilities.
What command I have to use plz help.
0 Kommentare
Akzeptierte Antwort
Wayne King
am 10 Feb. 2012
indices = randi(1e4,100,1);
x = zeros(100,100);
x(indices) = 1;
9 Kommentare
Walter Roberson
am 16 Feb. 2012
There are 65208469245472575695415972927215718683781335425416743372210247172869206520770178988927510291340552990847853030615947098118282371982392705479271195296127415562705948429404753632271959046657595132854990606768967505457396473467998111950929802400 different matrices
which is about 6.5 * 10^241
It *is* possible to list them all, if you do not need to store them, but it would take a *very* long time to do the listing.
Weitere Antworten (2)
Jan
am 16 Feb. 2012
You can add a test to Wayne's method to check if the indices are unique:
go = true;
while go
indices = randi(1e4,100,1);
go = numel(unique(inidices)) == 100;
% Or:
% go = all(diff(sort(indices)));
end
x = zeros(100,100);
x(indices) = 1;
Another approach, which works since Matlab 2011b:
indices = randperm(1e4, 100);
2 Kommentare
Walter Roberson
am 16 Feb. 2012
idx = randperm(100*100);
x = false(100,100); %you said you wanted a _binary_ matrix
x(idx(1:100)) = true;
If you have a new enough version of MATLAB,
idx = randperm(100*100,100);
x = false(100,100);
x(idx) = true;
Siehe auch
Kategorien
Mehr zu Logical 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!