Re: list combinations
- To: mathgroup@smc.vnet.net
- Subject: [mg11014] Re: list combinations
- From: Paul Abbott <paul@physics.uwa.edu.au>
- Date: Mon, 16 Feb 1998 18:15:21 -0500
- Organization: University of Western Australia
- References: <6c8r3a$b0d@smc.vnet.net>
Daniel Sanders wrote: > I have a bit of bother here about creating a list. The problem: Given a > set of lists of the same dimension, example > list={{1,2},{2,3},{5,6},{7,8}}, I want to join them in sets of two such > that every combination is joined. So, if list is of Dimensions={5,2}, > the new list will be of Dimensions={10,2,2}. Here is one way: In[1]:= list = {{1, 2}, {2, 3}, {5, 6}, {7, 8}, {9, 10}} Form the set of all possible pairs (somewhat inefficient for the following): In[2]:= Outer[{#1, #2} & , list, list, 1] Delete all pairs with duplicate First and Last elements: In[3]:= Union[Sort /@ Select[Flatten[%, 1], First[#1] =!= Last[#1] & ]] Out[3]= {{{1, 2}, {2, 3}}, {{1, 2}, {5, 6}}, {{1, 2}, {7, 8}}, {{1, 2}, {9, 10}}, {{2, 3}, {5, 6}}, {{2, 3}, {7, 8}}, {{2, 3}, {9, 10}}, {{ 5, 6}, {7, 8}}, {{5, 6}, {9, 10}}, {{7, 8}, {9, 10}}} In[4]:= Dimensions[%] Out[4]= {10,2,2} Here is another: In[5]:= Flatten[Table[{list[[i]], list[[j]]}, {i, Length[list]}, {j, i + 1, Length[list]}],1] Out[5]= {{{1, 2}, {2, 3}}, {{1, 2}, {5, 6}}, {{1, 2}, {7, 8}}, {{1, 2}, {9, 10}}, {{2, 3}, {5, 6}}, {{2, 3}, {7, 8}}, {{2, 3}, {9, 10}}, {{5, 6}, {7, 8}}, {{5, 6}, {9, 10}}, {{7, 8}, {9, 10}}} In[6]:= Dimensions[%] Out[6]= {10, 2, 2} Cheers, Paul ____________________________________________________________________ Paul Abbott Phone: +61-8-9380-2734 Department of Physics Fax: +61-8-9380-1014 The University of Western Australia Nedlands WA 6907 mailto:paul@physics.uwa.edu.au AUSTRALIA http://www.pd.uwa.edu.au/~paul God IS a weakly left-handed dice player ____________________________________________________________________