MATLAB: Matrix grouping result of combinations

matrix manipulation

given the list below:
listResult = combnk(1:6,2)
listResult =
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
how could I get an array (5 x 3) with the pairs above so that each row contains all elements of the set (1: 6). ex :
1 2 3 4 5 6
1 3 2 5 4 6
1 4 2 6 3 5
1 5 2 4 3 6
1 6 2 3 4 5

Best Answer

Hmm. How about this as a start, a constructive way to build the array you seem to be asking to create.
I'll presume that the goal is to start with the set of pairs nchoosek(1:n,2), where n is an even number. For example, given n=4, we get 6 pairs.
nchoosek(1:4,2)
ans =
1 2
1 3
1 4
2 3
2 4
3 4
Now we want to build an array with n-1 rows, and n columns, so an (n-1)xn array composed of those pairs.
Each of the n-1 rows must contain a permutation of the numbers 1:n. And since each row contains all numbers 1:n, and there will only be n-1 pairs in the set of all pairs generated that contain the number n, there can only be n-1 rows. And any pair can only be used once.
That tells us the first two columns, without loss of generality, can be created as:
1 2
1 3
...
1 n
Lets see what happens for n==4. There is now only one pair that can be used for the second column. So we can easily fill out the array.
1 2 3 4
1 3 2 4
1 4 2 3
This is the only way to build the array you are asking for when n==4. Any other solution can be viewed as a simple transposition that could be untangled by means of a sort of each pair. So we could exchange columns 3 and 4, for example, but that is not a distinct solution. Now, move to the case of n==6. (It is often the case that we can build intuition about a process by working through it with pencil and paper.) Thus, for n==6 we can start with:
1 2
1 3
1 4
1 5
1 6
We can exchange elements in any row of that set without changing anything significant. And we can randomly permute the rows. So, suppose that we now augment that to start like this:
1 3 2 6 4 5
1 2
1 4
1 5
1 6
There are 8 pairs remaining.
2 3
2 4
2 5
3 4
3 5
3 6
4 6
5 6
We can choose two of those pairs to fill out the second row. It could be done in one of these two ways:
1 2 3 4 5 6
1 2 3 5 4 6
1 2 3 6 4 5 ### Is excluded, since [4 5] has been used
So the problem now branches. That suggests one solution could be to solve this in a recursive manner, filling out the array one row at a time, but doing so recursively from the pairs remaining. Just doing that by pencil and paper, I could have gotten this array:
1 3 2 6 4 5
1 2 3 4 5 6
1 4 2 5 3 6
1 5 2 3 4 6
1 6 2 4 3 5
And there are other choices I could have made along the way. Each branch reflects a choice that I had to make, but there were some alternatives. For example, this next array shows one such alternative, where I changed the third row, by swapping the second and third pairs in that row.
1 3 2 6 4 5
1 2 3 4 5 6
1 4 3 6 2 5
1 5 2 3 4 6
1 6 2 4 3 5
So if you are asking how many ways this can be done, we can see that I can always swap pairs within any row.
So I'm still not positive what is your final goal. If the goal is to find one such array from the set of pairs nchoosek(1:n,2) for even n, then my answer is I would just build up that array one row at a time from the pairs remaining. This should be doable in a loop. Or, if you want to find ALL ways of creating that array, then I would use a recursive scheme. In any event, I don't think the code is difficult, it just requires care in how you write it, emulating the scheme you would use by pencil and paper and crossing out pairs that have been used already. If your real question is to know how many ways exist to distinctly create that array, then you need to decide which results are distinct. Thus, are these choices of rows distinct?
1 2 3 4 5 6
1 2 5 6 3 4
I might argue they are not distinct from what I think you seem to have said, since the same three pairs are used in both cases.