matrix rows combination with all possibilities

5 次查看(过去 30 天)
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 个评论
summyia qamar
summyia qamar 2017-1-12
it can be a one big cell with array of 6X7 and total of 64

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2017-1-12
编辑:James Tursa 2017-1-12
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 个评论
summyia qamar
summyia qamar 2017-1-12
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
Walter Roberson
Walter Roberson 2017-1-12
The code was designed assuming that the matrices were all the same number of rows.

请先登录,再进行评论。

更多回答(1 个)

John BG
John BG 2017-1-12
编辑:Jan 2017-1-15
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 个评论
summyia qamar
summyia qamar 2017-1-12
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by