Re: a random matrix of zeros and ones with exactly N ones



Steve Amphlett wrote:


Omid wrote:


How can I generate a random matrix of a defined size with
elements
zero or one with exactly M ones? For example a 4*5 matrix with
9
ones
and 11 zeros. I want it to be completely random such that the
generated matrix be able to span the space of all possible
permutations of zeros and ones (M ones)in the matrix.

You need <randperm>

x=ones(9,1);
x(20)=0;
x=reshape(x(randperm(20)),4,5)

And you can probably do it on one line if you want to be clever.

I don't want to be cleverer than you ;) Thanks for your help.
.



Relevant Pages