1

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

  • Set 1: choosing any 4 numbers from a vector of 13 elements.
  • Set 2: choosing any 2 numbers from a vector of 3 elements.
  • Set 3: choosing any 2 numbers from a vector of 9 elements.

Example: sample 3 from vector of 4 (H=3 and L=4) for the Set A, H=2 L=3 for the Set B and H=2 L=4 for the Set B:

           4    4   4
           3    4   4
           3    3   4
           3    3   3
           2    4   4
           2    3   4
           2    3   3
           2    2   4
Set A =    2    2   3
           2    2   2
           1    4   4
           1    3   4
           1    3   3
           1    2   4
           1    2   3
           1    2   2
           1    1   4
           1    1   3
           1    1   2
           1    1   1

           3    3
           2    3
Set B =    2    2
           1    3
           1    2
           1    1

           4    4
           3    4
           3    3
           2    4
Set C =    2    3
           2    2
           1    4
           1    3
           1    2
           1    1

[Set A] = [20 x 3], [Set B] = [6 x 2], [Set C] = [10 x 2]. Then I need to obtain all possible combinations from these three sets: AllComb = [Set A] x [Set B] x [Set C] = [1200 x 8]. The AllComb matrix will be like this:

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
                 .
                 .
                 .
1   1   1   |   1   1   |   1   1

Unfortunately I can not use the same number for the sets since I need to substitute the numbers like that:

  • For Set A: 1 = 10, 2 = 25, 3 = 30 and 4 = 45
  • For Set B: 1 = 5, 2 = 20 and 3 = 35
  • For Set C: 1 = 10, 2 = 20, 3 = 30 and 4 = 50

Any ideas? Real case sets will often lead to an AllComb matrix ~[491 400 x 8] so vectorized solutions will be gladly accepted.

Note: Each set is obtained with the following code:

a = combnk(1:H+L-1, H);
b = cumsum([a(:,1)  diff(a,[],2) - 1],2);

What is H and L?

H are the hoppers of a MultiheadWeigher (MHW) machines. I have a MHW with H=8 and I need to deliver in each of these hoppers some materials. If I need to deliver just one type of material all possibile combinations are (L+H-1)!/(H!(L-1)!) and i compute them with the code write above (a and b). Now, suppose to have 3 different product then we have 4 hoppers for product A, 2 for B and 2 for C. Product A in the first 4 hoppers can assume values 10:10:130, Product B 10:10:30 and c 10:10:90. Then the number of steps are for A L=13, B L=3 and C L=9

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
gmeroni
  • 571
  • 4
  • 16
  • I don't seem to understand your question: You have 3 vectors with `13,3,9` elements respectively. Then you take `4,2,2`random elements of these vectors (which result in "reduced" vectors) and with those elements you construct `Set A,B,C` which either have all permutations of 3 or 2 of those "reduced" vectors. And you got code for constructing the sets. What is your questions? – The Minion Jun 17 '14 at 08:42
  • I might be thick but what is the difference between this question and the question you asked yesterday? http://stackoverflow.com/q/24239505/2545927 – kkuilla Jun 17 '14 at 08:43
  • 1
    @kkuilla That question is about How to handle a huge single set matrix :) – gmeroni Jun 17 '14 at 08:51
  • @TheMinion Right now i have the code just to build a single set. I do not know how to combining them properly in order to obtain `AllComb` – gmeroni Jun 17 '14 at 08:52
  • SO you are able to compute the 3 Sets, where each row contains one possibility? And now you want to permutate over all those possibilities from 3 Sets? – The Minion Jun 17 '14 at 08:53
  • Nope, I can build a single set, in this case i can build separately A,b and C. I do not know how add up A,B and C togheter in order to obtain `AllComb` matrix. As you can see, first row of A is repeated 60 times, first row of B is repeated 10 times and so on...I'm able to do that with for, but i need a clever and fast solution. – gmeroni Jun 17 '14 at 08:59
  • I don't seem to understand your question either. What is `H` and `L`? What do they represent? Why can't you just pool the results? Where are you stuck? What is the issue? – kkuilla Jun 17 '14 at 09:00
  • @kkuilla `H` are the hoppers of a MultiheadWeigher (MHW) machines. I have a MHW with `H=8` and I need to deliver in each of these hoppers some materials. If I need to deliver just one type of material all possibile combinations are `(L+H-1)!/(H!(L-1)!)` and i compute them with the code write above (`a` and `b`). Now, suppose to have 3 different product then we have 4 hoppers for product `A`, 2 for `B` and 2 for `C`. Product `A` in the first 4 hoppers can assume values `10:10:130`, Product `B` `10:10:30` and `c` `10:10:90`. Then the number of steps are for `A` `L=13`, `B` `L=3` and `C` `L=9` – gmeroni Jun 17 '14 at 09:10
  • Please update your question with this information. If you could post an [mcve](http://stackoverflow.com/help/mcve) that would be great. @TheMinion this one is yours... – kkuilla Jun 17 '14 at 09:19
  • @kkuilla What do you mean? I like the challenge to find solutions to these kind of questions. If i find one i post it. But most times I don't get a solution so no reason to pass on this question :D – The Minion Jun 17 '14 at 09:25
  • @kkuilla The mcve is possibile only for the 1 Product case since the multiproduct is not solved yet :)! Building the `AllComb` matrix is the first step to solve this problem. – gmeroni Jun 17 '14 at 09:31
  • I meant, I give up.... :-) – kkuilla Jun 17 '14 at 09:57
  • @kkuilla I posted a solution for the given example: `[Set A] = [20 x 3], [Set B] = [6 x 2], [Set C] = [10 x 2]` – The Minion Jun 17 '14 at 09:59

2 Answers2

1

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.

The Minion
  • 1,164
  • 7
  • 16
1

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
     ...        
Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Nice!! I added some lines to substitute the results values with the real ones for each product (= set)! – gmeroni Jun 17 '14 at 12:32