MATLAB: Matrix rows combination with all possibilities

MATLABmatrix combination

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?

Best 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: