Find unique permutations of a matrix

19 views (last 30 days)
RW Student
RW Student on 22 Nov 2018
Edited: RW Student on 22 Nov 2018
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
Bruno Luong on 22 Nov 2018
Edited: 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.

Sign in to comment.

Answers (1)

Bruno Luong
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{:})
  5 Comments
Bruno Luong
Bruno Luong on 22 Nov 2018
Edited: Bruno Luong on 22 Nov 2018
How voltage/current criteria translates to the array values? sum(a,2) is the same? Or something more complicated such as Kirshoff's circuit law?
There are other numerical technique to optimize the arrangement on whatever criteria you want to optimize. IMHO going bruteforce is not a wise way to go.
RW Student
RW Student on 22 Nov 2018
Edited: RW Student on 22 Nov 2018
A solar panel consists of N solar cells. The arrangement of these solar cells in the panel should be optimized to maximize output power. The numbers in the matrix represent the solar cell index. First, I want to analyse in which unique ways (resulting in a different current/voltage) the cells could be connected together.
So I think the problem is almost solved. I have sorted the elements in each row for all elements of the cell array C using:
for i=1:numel(C)
C{i,1}=sort(C{i,1},2);
end
Now I only have to eliminate the duplicates. However, since C has a cell array within a cell array unique(C, 'rows') does not work. Any solution?

Sign in to comment.

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!