matrix rows combination with all possibilities

38 views (last 30 days)
summyia qamar
summyia qamar on 11 Jan 2017
Edited: Jan on 15 Jan 2017
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
summyia qamar
summyia qamar on 12 Jan 2017
it can be a one big cell with array of 6X7 and total of 64

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 12 Jan 2017
Edited: James Tursa on 12 Jan 2017
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
Walter Roberson
Walter Roberson on 12 Jan 2017
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

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!