I want to find all the possible combinations from a set of pairs. This example will help explaining the problem better. Say I have this line of code: c=nchoosek(1:6,2) , it gives:
c =
1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6
out=
Now I want to find all the possibles combinations using 3 pairs from the set c such that all the elements in the final array are unique. For example [1 2], [3 4] and [5 6] —-> [1 2 3 4 5 6] is a good combination [1 2 ], [1 3] and [5 6] —-> [1 2 1 3 5 6] is a combination I do not want because 1 is repetitive.
These are all the combinations that work
out =
1 2 3 4 5 6 1 2 3 5 4 6 1 2 3 6 4 5 1 3 2 4 5 6 1 3 2 5 4 6 1 3 2 6 4 5 1 4 2 3 5 6 1 4 2 5 3 6 1 4 2 6 3 5 1 5 2 3 4 6 1 5 2 4 3 6 1 5 2 6 3 4 1 6 2 3 4 5 1 6 2 4 3 5 1 6 2 5 3 4
Is there a fast way of doing this for c=nchoosek(1:n,2) and n is very large? I wrote a script that works fine using c1=nchoosek(1:size(c,1),3) when size(c,1) is not too large but the code is very slow when size(c,1) is very large.
If there is another way of doing this without nchoosek, the order of the tow pairs does not matter in the final result, for example [1 2 3 4 5 6} and [2 1 3 4 5 6] count as one combination.
Thanks
Best Answer