MATLAB: How to generate a random matrix with 0’s and 1’s

random matrixzeros and ones

Any help on generating a random matrix populated with zeros and ones, having exactly d =m/s ones per column, the locations of which are chosen uniformly from [m] without replacement. where m is the number of rows and s is from {1,…,m}. d must be an integer.
I have tried using randerr(m,n,d) but I can not get the desired output. Thanks

Best Answer

cell2mat(arrayfun(@(~) (randperm(m) <= d)', 1:n, 'UniformOutput', false))
would be one way.