Complete set of combinations combining 3 set

前端 未结 2 1223
面向向阳花
面向向阳花 2021-01-16 13:57

I need to generate the complete set of combinations obtained combining three different subset:

  • Set 1: choosing any 4 numbers from a vector of
相关标签:
2条回答
  • 2021-01-16 14:40

    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.

    0 讨论(0)
  • 2021-01-16 14:42

    You basically need to find

    1. Combinations with repetition for each set;
    2. "Multi-variations" (I don't know the correct name for this) of the results of stage 1.

    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
         ...        
    
    0 讨论(0)
提交回复
热议问题