generate random binary matrix under a condition
Show older comments
I want to generate a matrix with values that are either zero or one, but the sum of elements in each column is <=1. To generate a random m by n matrix, we use x=randi([0,1],m,n), but what to add to force the sum of elements in each column be <=1...that is only single '1' element in each column.
Accepted Answer
More Answers (1)
Andrei Bobrov
on 13 Mar 2015
x = zeros(5,7);
s = size(x);
x(sub2ind(s,randi([1 5],s(2),1),(1:s(2))')) = 1;
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!