generate random binary matrix under a condition
조회 수: 9 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Brendan Hamm
2015년 3월 13일
m = 5;
n = 6;
A = zeros(m,n);
for k = 1:n
c = randi([0,m]);
if c > 0
A(c,k) = 1;
end
end
댓글 수: 0
추가 답변 (1개)
Andrei Bobrov
2015년 3월 13일
x = zeros(5,7);
s = size(x);
x(sub2ind(s,randi([1 5],s(2),1),(1:s(2))')) = 1;
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!