How can I generate all permutations of a matrix, in which value "1" cannot be repeated in any column or row? Rest of the elements are identical, and a number between 0 and 1.
조회 수: 4 (최근 30일)
이전 댓글 표시
Each row must have at least an element with value 1.
Say we have 3 by 4 matrix. Then an acceptable arrangement would be:
x x 1 x
x x x 1
x 1 x x
and there will be:
4! / (4-3)! = 24
different allowed permutations of a 3 by 4 matrix.
Say for a 4 by 5 matrix, violations would be
1 x x x 1
x x x x x
x x x 1 x
x x x 1 x
where first row has more than one values of 1, and column 4 has the same violation. Lastly, there is no 1 value in row 2.
Similarly number of allowed permutations would be:
5! / (5-4)! = 120
that is, the number of different matrices.
My attempt is through different for loops, which is cumbersome for larger elements. Any smarter way to make this happen?
Thank you.
댓글 수: 0
채택된 답변
Roger Stafford
2016년 5월 21일
편집: Roger Stafford
2016년 5월 21일
I don’t know how you want to arrange all the matrices that are to be generated so I leave that aspect to you. Suppose you wish to generate all m-by-n matrices using the value x where m<=n. First create the matrix A:
A = toeplitz([1,repmat(x,1,m-1)],[1,repmat(x,1,n-1)]);
Next do this:
P = perms(1:m);
T = nchoosek(1:n,m);
C = zeros(size(T,1),n);
for k = 1:size(C,1)
C(k,[T(k,:),setdiff(1:n,T(k,:))]) = 1:n; % <-- Corrected
end
Now for every combination of ix in 1:size(P,1) and jx in 1:size(C,1) create
A(P(ix,:),C(jx,:))
There will be n!/(n-m)! of these altogether. If m>n, do this the other way around.
댓글 수: 2
Roger Stafford
2016년 5월 21일
I’m afraid I made an error on that code for C. I have corrected it in my answer. As it stood it would duplicate certain matrices and omit others.
추가 답변 (1개)
참고 항목
카테고리
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!