How to distribute 100 ones in 100 by 100 matrix randomly in all posibilities
Show older comments
I want to make binary matrix, and distribute 100 ones in it, randomly in all possibilities.
What command I have to use plz help.
Accepted Answer
More Answers (2)
Jan
on 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 Comments
Amit
on 16 Feb 2012
Walter Roberson
on 16 Feb 2012
What do you mean by "it is still varying" ?
Walter Roberson
on 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;
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!