How to distribute 100 ones in 100 by 100 matrix randomly in all posibilities

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

indices = randi(1e4,100,1);
x = zeros(100,100);
x(indices) = 1;

9 Comments

Hey Thanks Wanyne
Can you please tell me how I can find out the maximum number of possibilities of distributionb by matlab command.
well there are 10,000 ways to place the first number, 9,999 ways to place the second, 9,998 ways to place the third, and so on down to 9901, that's a very big product!!! You can use factorial(), but I think you're going to get such a large number here that it will probably blow up and return Inf
nchoosek(1e4,100) gives you the number of ways of choosing 100 out of 10,000 where order is NOT important (that is not what you're asking here, what you're asking is bigger). But even nchoosek(1e4,100) is going to throw a warning because it's so large.
while using above code, I am facing a problem i.e. the mean of same matrix (different one's position inside) is changing by doing it again and again.
can you please tell me another command
Amit, if by mean of the matrix, you intend to say mean(x), which gives the mean of the columns, then of course it has to change because you are randomly placing ones, so the locations of the ones in the columns changes.
Using RANDI does not ensure that the indices are unqiue. Therefore you cannot be sure, that you really get 100 ones.
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.
What command I have to use that gives me exactly 100 ones randomly in 100 by 100 binary matrix

Sign in to comment.

More Answers (2)

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);
If this is time-critical, you can try FEX: Shuffle.
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;

Products

Asked:

on 10 Feb 2012

Edited:

on 15 Oct 2013

Community Treasure Hunt

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

Start Hunting!