MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: How to construct all possible orderings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15638] RE: [mg15588] How to construct all possible orderings
  • From: "ELLIS, Luci" <EllisL at rba.gov.au>
  • Date: Sat, 30 Jan 1999 04:28:36 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Carlos,
Try the Table[] command and a couple of replacement rules:
Table[{i,j,k},{i,3},{j,3},{k,3}]/. {1->"A",2->"B",3->"C"} This will
give you a 3*3*3*3 tensor. To get a vector of "ABC"-type strings, Apply
StringJoin to the lowest level (which will probably be 3, but check
this, I don't have Mathematica on this machine to check it myself) and
then Flatten.

So the final function will look like:
 Flatten[Apply[StringJoin,Table[{i,j,k},{i,3},{j,3},{k,3}]/.
{1->"A",2->"B",3->"C"},{3}]]

For the general case with N elements, the replacement rule list will
have to be a Table[] or Array command and the Table will have depth n
eg something like:
orderings[n_Integer]/;n>0 :=
With[{iters=Transpose[{Array[i,n],Table[n,{n}]}] , elem = Array[i,n]}, 
(*  can objects of the form i[1] be used as an iterator? I don't know
and can't test it here.  *)
Flatten[Apply[StringJoin, Map[ToString, Table[elem, Evaluate [Sequence
@@ iters] ]/. Thread[Range[n]->Array[A,{n}]],{-1}]
	,{n}]] ]

Of course, the analytical solution is that there are n^n combinations,
so this could get pretty huge for even moderate n.

Hope this helps,
Luci
____________________________________________________ Luci Ellis         
ph:61-2-9551-8881 Acting Senior Economist            fx:61-2-9551-8833
Financial & Monetary Conditions    ellisl at rba.gov.au Economic Analysis
Department       GPO Box 3947 Reserve Bank of Australia          Sydney
NSW 2001


-----Original Message-----
From: Carlos Carreto [mailto:ccarreto at ipg.pt] To: mathgroup at smc.vnet.net
Subject: [mg15638] [mg15588] How to construct all possible orderings 


*** This E-Mail has been checked by MAILsweeper *** Hello,

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

How can I do this? 

Thank you.

-:- Carlos



  • Prev by Date: Re: question
  • Next by Date: Re: Vector Analysis
  • Previous by thread: Re: How to construct all possible orderings
  • Next by thread: Re: How to construct all possible orderings