I wanted to find all combinations of elements from a set of vectors. I found the following answer which works perfectly. However, some of my vectors are paired together. For instance, if I have the vector [15, 20]
and [60, 70]
, I would like to get the combination [15, 60]
and [20, 70]
only (because 15
can't be combined with 70
).
Hence, for the following vectors:
vectors = {[1 2], [3 6 9], [10 20 30]} % [3 6 9] and [10 20 30] are paired
, should give
combs = [ 1 3 10
1 6 20
1 9 30
2 3 10
2 6 20
2 9 30 ]
For this simple example, I'm able to use the combination code from the link by using vectors = {[1 2], [3 6 9]}
, and by doing a concatenation for generating the third column:
combs = [combs, repmat([10 20 30], 1, size(combs, 1)/size([10 20 30], 2))'];
However, my cases are not that simple. For example, I would to have a code that works for a vectors:
vectors = {[1 2], [3 6 9], [10 20 30], [3 4 5], [55 66 77], [555 666 777], [101 201]}
% [3 6 9] and [10 20 30] are a pair.
% [55 66 77] and [555 666 777] are a pair.