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 views (last 30 days)
Show older comments
Bidsitlov
on 21 May 2016
Commented: Roger Stafford
on 21 May 2016
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 Comments
Accepted Answer
Roger Stafford
on 21 May 2016
Edited: Roger Stafford
on 21 May 2016
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 Comments
Roger Stafford
on 21 May 2016
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.
More Answers (1)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!