I have N data elements (say, 1:N) that I want to distribute into two distinct groups. I do not know what the best distribution is, so I want to test every possible pairing, but ignore symmetric results.
I need a function that has such a result:
>> pairings(1:2)
{
[1], [2]
}
>> pairings(1:3)
{
[1], [2,3]
[2], [1,3]
[3], [1,2]
}
>> pairings(1:4)
{
[1], [2,3,4]
[2], [1,3,4]
[3], [1,2,4]
[4], [1,2,3]
[1,2], [3,4]
[1,3], [2,4]
[1,4], [2,3]
}
Of course an algorithm that employs a lot of looping would be easy enough to write, but I guess there is a solution that does feel more like matlab.
This is essentially a power set enumeration problem. Assume that the element
1is in the first of the two sets. Enumerate the power set of the elements2 .. N. This can be done by looping through the binary numbers from1 .. 2^(N-1). Add the element1to the result set as the first set in the pair. Take the complement of the result set as your other set. Toss away the empty set as a subset if it’s not relevant for your problem.