MATLAB: How to generate a RANDOM matrix of 0’s and 1’s such that the number of 1’s in any column is the same.

random number generator

The positioning of 0's and 1's across the matrix should be random. At the same time, the number of 1's should be fixed. Something like this {1 0 1 0 1 0; 0 1 0 1 0 0; 1 0 1 0 1 0; 0 1 0 1 0 1; 0 0 0 0 0 1 0 0 0 0 0 0} I tried using randperm randi .but I am not getting equal number of 1's Thanks in advance

Best Answer

n=6
A=zeros(n)
m=2 % number of 1 per column
for k=1:n
idx=randperm(n,m)
A(idx,k)=1
end