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.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Bidsitlov
am 21 Mai 2016
Kommentiert: Roger Stafford
am 21 Mai 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 Kommentare
Akzeptierte Antwort
Roger Stafford
am 21 Mai 2016
Bearbeitet: Roger Stafford
am 21 Mai 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 Kommentare
Roger Stafford
am 21 Mai 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.
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!