Complete set of combinations combining 3 set

前端 未结 2 1224
面向向阳花
面向向阳花 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.

提交回复
热议问题