matrix rows combination with all possibilities

I have 6 matrix, each with 2 rows. the number of combinations that can be generated are 2^6=64 how can I generate the set of all possible combinations of the rows of each matrix such that no 2 rows of one matrix appear in one combination for example
P1=[1 0 0 1 0 1 1;1 1 0 0 1 0 1];
P2=[0 1 1 1 0 0 1;1 0 1 0 1 1 0];
P3=[1 0 0 1 1 0 0;0 1 1 0 0 0 1];
P4=[1 0 0 0 1 0 1;0 1 0 1 0 1 0];
P5=[1 1 0 0 0 1 0;1 1 0 0 1 0 1];
P6=[0 1 0 0 0 1 1;1 1 0 1 0 1 0];
these are set of matrices. the possible combination can be (P1R1,P2R1,P3R1,P4R1,P5R2,P6R1)..I tried combvec but it gave one complete matrix by combining all. Is there any function in matlab or any code?

3 Comments

Do you want your results in one big array, or do you want to generate the result at each iteration of a loop?
it can be a one big cell with array of 6X7 and total of 64

Sign in to comment.

 Accepted Answer

E.g., one way to generate the possible combinations all in one 3D matrix, where each 2D plane (the first two dimensions) have the P data:
P = [P1;P2;P3;P4;P5;P6];
m = 6;
n = 2^m-1;
Pset = zeros(6,size(P1,2),n+1);
for k=0:n
Pset(:,:,k+1) = P((1:2:2*m-1)+(dec2bin(k,m)-'0'),:);
end
The result is in the variable Pset.
* EDIT *
A more generic version for the case where the individual P's can have different number of rows. Same basic approach, but uses allcomb (by Jos from the FEX) instead of dec2bin:
P = [P1;P2;P3;P4;P5;P6];
z = [size(P1,1) size(P2,1) size(P3,1) size(P4,1) size(P5,1) size(P6,1)];
c = [0 cumsum(z(1:end-1))];
a = allcomb(1:z(1),1:z(2),1:z(3),1:z(4),1:z(5),1:z(6));
n = size(a,1);
Pset = cell(1,n);
for k=1:n
Pset{k} = P(c+a(k,:),:);
end
You can find allcomb here:

10 Comments

cant understand this..how to generate P?
P is just a big matrix will all of the individual P1, P2, ... P6 concatenated together. For convenience in downstream indexing.
ok.. can we get the output in form of cell array? because these individual (6x7) matrices are used as input to another loop
Pset = cell(1,n+1);
for k=0:n
Pset{k+1} = P((1:2:2*m-1)+(dec2bin(k,m)-'0'),:);
end
when running Turnsa's code, the resulting combinations cannot this short
P =
1 0 0 1 0 1 1
1 1 0 0 1 0 1
0 1 1 1 0 0 1
1 0 1 0 1 1 0
1 0 0 1 1 0 0
0 1 1 0 0 0 1
1 0 0 0 1 0 1
0 1 0 1 0 1 0
1 1 0 0 0 1 0
1 1 0 0 1 0 1
0 1 0 0 0 1 1
1 1 0 1 0 1 0
P is longer, much longer.
John, I do not understand your comment? The user gave P1, P2, P3, P4, P5, P6, each of which is 2 rows. James created P=[P1;P2;P3;P4;P5;P6] for use in his algorithm; since it is 6 matrices of 2 rows each, it is going to have 6*2 = 12 rows. It is not going to be "longer, much longer".
James uses P as a temporary variable in the calculation. The output he creates is Pset. In the cell array form of his code, the cell is 64 items long, each 6 x 7, exactly in accordance with the user's request "it can be a one big cell with array of 6X7 and total of 64"
Mr Robertson
thanks for pointing out the other possibility:
In my answer, replace the line
S=combinator(11,6,'p');
with
S=combinator(11,6,'c');
now one gets
size(P)
ans =
3265 6
which is still long long longer than the resulting P with Mr Tursa's code, isn't it?
Again: P is a temporary variable in James' code, not the output. The output is Pset.
The user's question starts with:
"I have 6 matrix, each with 2 rows. the number of combinations that can be generated are 2^6=64"
So any answer that does not produce 64 combinations is not the answer the user is looking for.
for the given set of matrices, the code provided in answer is not working
P1=[1 0 0 1 0 1 1];
P2=[0 1 1 1 0 0 1];
P3=[1 0 0 1 1 0 0];
P4=[1 0 0 0 1 0 1];
P5=[1 1 0 0 0 1 0;1 1 0 0 1 0 1];
P6=[0 1 0 0 0 1 1;1 1 0 1 0 1 0];
although I made changes in code as below
P=vertcat(P1,P2,P3,P4,P5,P6);
n= 2^2-1;
all_comb_ofRoutes = zeros(No_of_Parts,size(P1,2),n+1);
for i=0:n
all_comb_ofRoutes(:,:,i+1) = P((1:1:7)+(dec2bin(i,No_of_Parts)-'0'),:);
end
all_comb_ofRoutes = cell(1,n+1);
for j=0:n
all_comb_ofRoutes{j+1} = P((1:1:7)+(dec2bin(j,No_of_Parts)-'0'),:);
end
but the error is Error using + Matrix dimensions must agree... how to generalize this thing so that by adding each row in every matrix gives the result
The code was designed assuming that the matrices were all the same number of rows.

Sign in to comment.

More Answers (1)

John BG
John BG on 12 Jan 2017
Edited: Jan on 15 Jan 2017
Summyia
1.
instead of binary, let me use the decimal figures:
B1=[sum(2.^[6:-1:0].*P1(1,:));sum(2.^[6:-1:0].*P1(2,:))]
B2=[sum(2.^[6:-1:0].*P2(1,:));sum(2.^[6:-1:0].*P2(2,:))]
B3=[sum(2.^[6:-1:0].*P3(1,:));sum(2.^[6:-1:0].*P3(2,:))]
B4=[sum(2.^[6:-1:0].*P4(1,:));sum(2.^[6:-1:0].*P4(2,:))]
B5=[sum(2.^[6:-1:0].*P5(1,:));sum(2.^[6:-1:0].*P5(2,:))]
B6=[sum(2.^[6:-1:0].*P6(1,:));sum(2.^[6:-1:0].*P6(2,:))]
B=[B1 B2 B3 B4 B5 B6]
=
75 57 76 69 98 35
101 86 49 42 101 106
2.
You want to find certain combinations, not all of them.
James Turnsa code starts well be it's too selective. With the function combinator.m, available from the File Exchange. for convenience I have appended copy of combinator.m at the end of these lines.
S contains all permutations without repetition of 11 elements over 6 bins.
S=combinator(11,6,'p');
3.
You want variable P a single matrix to contain all results, initialising
P=zeros(1,6);
4.
Now, for each pair, let's pull first 1st row, find all permutations, then pull second row, find again all permutations, a so on for all 6 columns of B, this excluding the column of B being selected, precisely to avoid including permutations that you don't really want in the result:
for k=1:1:6
C=B(:,k);
D=B;
D(:,k)=[];
D=D(:)';
pivot=C(1);
L=[pivot D];
P=[P;L(S)];
% for n=1:1:size(S,1) % it takes over minute
% P=[P;L(S(n,:))];
% end
% P=unique(P,'rows') % no need here
pivot=C(2);
L=[pivot D];
P=[P;L(S)];
end
5.
Eliminating the initialisation
P(1,:)=[];
Now P has 3991680 rows. Some rows have repeated when pivoting. Eliminating repeated permutations:
P=unique(P,'rows');
Now P has 408240 rows
6.
To obtain the combinations instead of permutations, replace line
S=combinator(11,6,'p');
with
S=combinator(11,6,'c');
now one would get P with size
size(P)
ans =
3265 6
7.
Observation: there are 2 repeated values P2(2) and P5(2) are both in decimal
101
which means there are going to be repeated values despite you are asking to avoid repeating rows.
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
[EDITED, Jan, Code of Matt Fig's combinator.m removed]

2 Comments

thankyou so much sir for your effort. I got the result from sir James method.. basically my idea is if I have 6 groups and I want combination of each element in them, then the formula to calculate possible combinations is 2*2*2*2*2*2 means for 1st group combination with 2nd, there are (2*2=4) possibilities then for these 4 possibilities to combine with the 3rd group is (4*2=8) and so on... Please guide me if I'm wrong in this.although I have generated 64 combinations of this which I like to share
cool, thanks for the exmplanation

Sign in to comment.

Products

Asked:

on 11 Jan 2017

Edited:

Jan
on 15 Jan 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!