I need to generate the complete set of combinations obtained combining three different subset:
I guess this could be further optimized but this generates you AllComb:
H=3;
L=4;
a = combnk(1:H+L-1, H);
b = cumsum([a(:,1) diff(a,[],2) - 1],2);
H=2;
L=3;
c = combnk(1:H+L-1, H);
d = cumsum([c(:,1) diff(c,[],2) - 1],2);
H=2;
L=4;
e = combnk(1:H+L-1, H);
f = cumsum([e(:,1) diff(e,[],2) - 1],2);
u=[];
for k=1:10
u=vertcat(u,d);
end
u=sortrows(u,[1 2]);
v=[];
for k=1:6
v= vertcat(v,f);
end
w= [u,v];
v=[];
for k=1:20
v= vertcat(v,w);
end
u=[];
for k=1:60
u = vertcat(u,b);
end
u=sortrows(u,[1 2 3]);
AllComb= [u,v];
Here b,d and f are your 3 sets. Then i loop over the numbers of permutation in d and f and replicate them so that all possibilities are constructed. One of them is sorted and then i write them in a new matrix w. THis process is repeated with Set A (b) and this new constructed matrix. Resulting in the end in AllComb.
You basically need to find
Both stages can be solved with more or less the same logic, taken from here.
%// Stage 1, set A
LA = 4;
HA = 3;
SetA = cell(1,HA);
[SetA{:}] = ndgrid(1:LA);
SetA = cat(HA+1, SetA{:});
SetA = reshape(SetA,[],HA);
SetA = unique(sort(SetA(:,1:HA),2),'rows');
%// Stage 1, set B
LB = 3;
HB = 2;
SetB = cell(1,HB);
[SetB{:}] = ndgrid(1:LB);
SetB = cat(HB+1, SetB{:});
SetB = reshape(SetB,[],HB);
SetB = unique(sort(SetB(:,1:HB),2),'rows');
%// Stage 1, set C
LC = 4;
HC = 2;
SetC = cell(1,HC);
[SetC{:}] = ndgrid(1:LC);
SetC = cat(HC+1, SetC{:});
SetC = reshape(SetC,[],HC);
SetC = unique(sort(SetC(:,1:HC),2),'rows');
%// Stage 2
L = 3; %// number of sets
result = cell(1,L);
[result{:}] = ndgrid(1:size(SetA,1),1:size(SetB,1),1:size(SetC,1));
result = cat(L+1, result{:});
result = reshape(result,[],L);
result = [ SetA(result(:,1),:) SetB(result(:,2),:) SetC(result(:,3),:) ];
result = flipud(sortrows(result)); %// put into desired order
This gives
result =
4 4 4 3 3 4 4
4 4 4 3 3 3 4
4 4 4 3 3 3 3
4 4 4 3 3 2 4
4 4 4 3 3 2 3
4 4 4 3 3 2 2
4 4 4 3 3 1 4
4 4 4 3 3 1 3
4 4 4 3 3 1 2
4 4 4 3 3 1 1
4 4 4 2 3 4 4
4 4 4 2 3 3 4
4 4 4 2 3 3 3
4 4 4 2 3 2 4
4 4 4 2 3 2 3
4 4 4 2 3 2 2
4 4 4 2 3 1 4
4 4 4 2 3 1 3
4 4 4 2 3 1 2
...