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



Omid <omid@xxxxxxxxxxxxx> writes:

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.

I would use

m = 4;
n = 5;
M = 9;
tmp = [ones(1,M) zeros(1,m*n - M)];
rp = randperm(m*n);
x = reshape(tmp(rp), [m n])

--
Peter Boettcher <boettcher@xxxxxxxxxx>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/
.



Relevant Pages