Re: How to construct all possible orderings
- To: mathgroup at smc.vnet.net
- Subject: [mg15674] Re: How to construct all possible orderings
- From: "Stephen P Luttrell" <luttrell at signal.dra.hmg.gb>
- Date: Sat, 30 Jan 1999 04:29:06 -0500 (EST)
- Organization: Defence Evaluation and Research Agency
- References: <78patb$ctc@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
>I am trying to construct all possible orderings of for example 3 >elements, where each element can be A, B or C. > >The result will be something like this: AAA >AAB >AAC >ABA >. . . >CCC The following code does what you want: Define a function to pick triplets of elements from {A,B,C}: In[1]:=f[i_,j_,k_]:={#[[i]],#[[j]],#[[k]]}&[{A,B,C}] This could easily be generalised to pick n-tuples of elements from any list. Now construct a nested list of the lists that you seek: In[2]:=Array[f,{3,3,3}] Out[2]={{{{A,A,A},{A,A,B},{A,A,C}},{{A,B,A},{A,B,B},{A,B,C}},{{A,C,A},{A,C,B },{A,C, C}}},{{{B,A,A},{B,A,B},{B,A,C}},{{B,B,A},{B,B,B},{B,B,C}},{{B,C,A},{B, C,B},{B,C,C}}},{{{C,A,A},{C,A,B},{C,A,C}},{{C,B,A},{C,B,B},{C,B,C}},{{ C,C,A},{C,C,B},{C,C,C}}}} Now flatten it to make it look even more like what you seek: In[37]:=Flatten[%,2] Out[37]={{A,A,A},{A,A,B},{A,A,C},{A,B,A},{A,B,B},{A,B,C},{A,C,A},{A,C,B},{A, C,C},{B,A, A},{B,A,B},{B,A,C},{B,B,A},{B,B,B},{B,B,C},{B,C,A},{B,C,B},{B,C,C},{C,A, A},{C,A,B},{C,A,C},{C,B,A},{C,B,B},{C,B,C},{C,C,A},{C,C,B},{C,C,C}} I suspect that there might be a clever routine in DiscreteMath`Combinatorica` that does this directly, but I can't find it. Stephen P Luttrell Signal Processing and Imagery Department DERA Malvern, St.Andrew's Road Malvern, United Kingdom, WR14 3PS +44 (0)1684 894046 (tel) +44 (0)1684 894384 (fax) luttrell at signal.dera.gov.uk (email)