Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list {1, 2, 3, 4} and wish to form

 {{{1,2},{3,4}}, 
 {{1,3},{2,4}}, 
 {{1,4},{2,3}}}

I ended up doing it manually like this:

a={1,2,3,4}
{{{a[[1]],a[[2]]}, {a[[3]],a[[4]]}},
{{a[[1]],a[[3]]}, {a[[2]],a[[4]]}},
{{a[[1]],a[[4]]}, {a[[2]],a[[3]]}}}

Is there a way to do this more simply? Of course this is a simplified question from the actual problem which is more complicated. An answer to this can be generalized to my actual problem. Thanks.

share|improve this question
    
Can you say which rule results in the partitioning ? If there is one you would apply it to Subsets[{1, 2, 3, 4}, {2}]. –  b.gatessucks Nov 2 '12 at 12:57
5  
Does this question/answer about Bell numbers do what you expect? If yes, then this is a dupe. –  István Zachar Nov 2 '12 at 13:04
    
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2)Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` –  chris Nov 2 '12 at 14:37
1  
Show your actual problem. Your remark about generalizing is not likely to be correct, because different methods for this will have different limitations. –  Daniel Lichtblau Nov 2 '12 at 16:35
add comment

2 Answers

Needs["Combinatorica`"]
Cases[KSetPartitions[{1, 2, 3, 4}, 2], ConstantArray[{_, _}, 2]]

=>

{

{{1, 2}, {3, 4}},

{{1, 4}, {2, 3}},

{{1, 3}, {2, 4}}

}

Extending the method to a list with 6 elements:

Cases[KSetPartitions[{1, 2, 3, 4, 5, 6}, 3], ConstantArray[{_, _}, 3]]

=>

{

{{1, 2}, {3, 4}, {5, 6}}, {{1, 2}, {3, 6}, {4, 5}},

{{1, 2}, {3,5}, {4, 6}}, {{1, 4}, {2, 3}, {5, 6}},

{{1, 3}, {2, 4}, {5, 6}}, {{1, 6}, {2, 3}, {4, 5}},

{{1, 3}, {2, 6}, {4, 5}}, {{1, 5}, {2,3}, {4, 6}},

{{1, 3}, {2, 5}, {4, 6}}, {{1, 6}, {2, 5}, {3, 4}},

{{1, 5}, {2, 6}, {3, 4}}, {{1, 6}, {2, 4}, {3, 5}},

{{1,4}, {2, 6}, {3, 5}}, {{1, 5}, {2, 4}, {3, 6}},

{{1, 4}, {2, 5}, {3, 6}}

}

Edit

See Rojo's answer to this question for a much better method.

share|improve this answer
add comment

When I understood right what you try to achieve, then yes, there are some ways. Whether it still works in your real life example is another thing. Let me give two examples:

1. Method

Select[Nest[Subsets[#, {2}] &, {1, 2, 3, 4}, 2], 
 Intersection @@ # === {} &]

Here, we create first all possible subsets of length 2 of {1,2,3,4}. Then we do this step again which leaves us with elements of the form {{1, 4}, {2, 3}} but, there are cases when a number which is in the first part appears in the last part too. Therefore, we select only those elements, where the Intersection of the first and the last part is the empty set.

2. Method

Union[Map[Sort, Partition[#, 2] & /@ Permutations[{1, 2, 3, 4}], -2]]

Here, we create all permutations which will probably not work in your real case. Than we partition those permutations into the correct form. This contains the elements {{1, 2}, {3, 4}} and {{1, 2}, {4, 3}}. Therefore, we sort all first and last parts and delete the duplicates with Union.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.