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




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.

Another solution to add to the list:

When I needed to do something similar, I used SPRAND, which
takes arguments m, n, and density. The argument claims that
there are only approximately m*n*density nonzero values, but I found
that the value was exact (if it is an integer).

So you might experiment with something like:

m=4;
n=5;
M = 9;
density = M/m*n;
A = full(sign(sprand(m,n,density)));

- Randy

.



Relevant Pages