Advance combination of sets and subsets
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi
I am having trouble in solving a combination problem as follows, For example:
There are 4 sets each having different sub parts
set(A) = ['A1' 'A2' 'A3' 'A4' 'A5' 'A6'];
set(B) = ['B1' 'B2' 'B3' 'B4' 'B5'];
set(C) = ['C1' 'C2' 'C3' 'C4'];
set(D) = ['D1' 'D2' 'D3' 'D4'];
Based original code of combination:
combos = combntns(set,subset);
now the question is: I have sub part of sets and I want combination of subparts of sets for subset=4 ,where same sub parts of a set should not be in the same combination. The problem is that by running original code, sub parts of single set also is getting combined, but this is not the right for me. the result should be as follow:
['A1' 'B1' 'C1' 'D1'],
['A1' 'B2' 'C1' 'D1'],
['A1' 'B3' 'C1' 'D1'],
['A2' 'B1' 'C1' 'D3'],
['A3' 'B3' 'C2' 'D3'],
['A5' 'B5' 'C4' 'D2'],
...
AND GO ON.....
but original code creates this: [A1 A2 C1 D1], or [A1 B1 B2 D1], ..... which is not true because sub part of set is repeated in single combination (for example B1 and B2 in the same combination).
i would appreciate if someone can put me through right direction.
Regards
0 Commenti
Risposte (1)
the cyclist
il 29 Apr 2014
Modificato: the cyclist
il 29 Apr 2014
Here is one straightforward implementation:
% Put in actual values here. I just used 1:6, etc, for testing.
A = 1:6;
B = 1:5;
C = 1:4;
D = 1:3;
S = nan(numel(A)*numel(B)*numel(C)*numel(D),4);
rowcount = 0;
for i1 = 1:numel(A)
for i2 = 1:numel(B)
for i3 = 1:numel(C)
for i4 = 1:numel(D)
if not(i1==i2 || i1==i3 || i1==i4 || i2==i3 || i2==i4 || i3==i4)
rowcount = rowcount + 1;
S(rowcount,:) = [A(i1) B(i2) C(i3) D(i4)];
end
end
end
end
end
S(rowcount+1:end,:) = [];
Notice that I preallocate quite a bit more memory than I need, because I was too lazy to figure out how many rows you'll need. I think it might be (shortest vector)^(number of sets), but I am not sure.
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!