Find unique permutations of a matrix
19 views (last 30 days)
Show older comments
I want to construct all possible matrices with N elements. Example: if N=6, then I want a 1x6, 2x3 3x2 and 6x1 matrix. These matrices should all be unique. So if we consider a 2x3 matrix, the options would be: [1 2 3; 4 5 6], [1 2 4; 3 5 6], [1 2 5; 3 4 6], [1 2 6; 3 4 5], [1 3 4; 2 5 6], [1 3 5; 2 4 6], etc. For this matrix there will be 10 unique combinations. A similar expression for a 3x2 matrix can be obtained: [1 2; 3 4; 5 6], [1 2; 3 5; 4 6], [1 2; 3 6; 4 5], [1 3; 2 4; 5 6], etc. For this matrix there will be 15 unique combinations. The 1x6 and 6x1 matrices only contain 1 unique combination each. Note that the order of rows and the order within each row does not matter and each number can only be used once! So far, I did this manually for N=1:7. For a larger number of N, the number of combinations will explode. For N=12, there will be 32034 combinations possible...
I already did several attempts to obtain all possible matrices and searched for people who tackled similar problems . However, these options do not give the desired results. Can someone please help me? Thank you in advance!
1 Comment
Bruno Luong
on 22 Nov 2018
"For a larger number of N, the number of combinations will explode. For N=12, there will be 32034 combinations possible..."
At least you realize you are engaging in the bad way solving of whatever the problem you want to solve.
I count 2874009600 combinations possible.
Answers (1)
Bruno Luong
on 22 Nov 2018
N = 6;
p = factor(N);
q = length(p);
b = dec2bin(0:2^q-1,q)-'0';
m = unique(prod(b.*p+(1-b),2));
n = N./m;
C = num2cell(perms(1:N),2);
reshapefun = @(m,n) cellfun(@(A) reshape(A,[m n]), C, 'unif', 0);
C = arrayfun(@(m,n) reshapefun(m,n), m, n, 'unif', 0);
C = cat(1,C{:})
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!