How to generate a binary matrix with a fixed sum in rows and a changeable sum in columns?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello everyone !
i have a binary matrix (M,N) .I want to get sum equal to 1 in each row and in columns sum varies between [1 and 6], the sum of all matrix equal to M.
i will be grateful if you could help me
댓글 수: 3
Torsten
2022년 8월 22일
What about
[1 0
1 0
0 1
0 1
0 1
0 1]
?
Do you see how to construct one such matrix ?
Of course, you can permute the rows arbitrarily - the resulting matrix will still fulfill the requirements.
답변 (1개)
Bruno Luong
2022년 8월 21일
편집: Bruno Luong
2022년 8월 21일
A=kron(eye(5),ones(3,1));
A=A(randperm(end),randperm(end))
sum(A,1)
sum(A,2)
댓글 수: 4
Bruno Luong
2022년 8월 22일
편집: Bruno Luong
2022년 8월 22일
The first line create the specific matrix that meet the row/column sum requirements.
The second line permutes rows and columns randomly, create a new random matrix, the sums are invariants under these operations, so the new matrix still meets the sum requirements.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!