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.
3 次查看(过去 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!