Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: RE: Combinations

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49761] Re: [mg49706] RE: [mg49672] Combinations
  • From: "E. Martin-Serrano" <eMartinSerrano at houston.rr.com>
  • Date: Fri, 30 Jul 2004 06:02:14 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Gregory,

The following seems to be a generalization of David's solution for input of
any length.

MyCombinations[list_List]:=
  Module[{step1},
 
step1=Flatten[Outer[List,Sequence@@Array[list&,Length[list]]],Length[list]-1
];
     StringJoin[#1]&/@( Map[SymbolName,#]&/@step1)
    ]


In[1]:= MyCombinations[{a,b,c,d}]

Out[1]=
{aaaa,aaab,aaac,aaad,aaba,aabb,aabc,aabd,aaca,aacb,aacc,aacd,aada,aadb,aadc,
aadd,abaa,abab,abac,abad,abba,abbb,abbc,abbd,abca,abcb,abcc,abcd,abda,abdb,a
bdc,abdd,acaa,acab,acac,acad,acba,acbb,acbc,acbd,acca,accb,accc,accd,acda,ac
db,acdc,acdd,adaa,adab,adac,adad,adba,adbb,adbc,adbd,adca,adcb,adcc,adcd,add
a,addb,addc,addd,baaa,baab,baac,baad,baba,babb,babc,babd,baca,bacb,bacc,bacd
,bada,badb,badc,badd,bbaa,bbab,bbac,bbad,bbba,bbbb,bbbc,bbbd,bbca,bbcb,bbcc,
bbcd,bbda,bbdb,bbdc,bbdd,bcaa,bcab,bcac,bcad,bcba,bcbb,bcbc,bcbd,bcca,bccb,b
ccc,bccd,bcda,bcdb,bcdc,bcdd,bdaa,bdab,bdac,bdad,bdba,bdbb,bdbc,bdbd,bdca,bd
cb,bdcc,bdcd,bdda,bddb,bddc,bddd,caaa,caab,caac,caad,caba,cabb,cabc,cabd,cac
a,cacb,cacc,cacd,cada,cadb,cadc,cadd,cbaa,cbab,cbac,cbad,cbba,cbbb,cbbc,cbbd
,cbca,cbcb,cbcc,cbcd,cbda,cbdb,cbdc,cbdd,ccaa,ccab,ccac,ccad,ccba,ccbb,ccbc,
ccbd,ccca,cccb,cccc,cccd,ccda,ccdb,ccdc,ccdd,cdaa,cdab,cdac,cdad,cdba,cdbb,c
dbc,cdbd,cdca,cdcb,cdcc,cdcd,cdda,cddb,cddc,cddd,daaa,daab,daac,daad,daba,da
bb,dabc,dabd,daca,dacb,dacc,dacd,dada,dadb,dadc,dadd,dbaa,dbab,dbac,dbad,dbb
a,dbbb,dbbc,dbbd,dbca,dbcb,dbcc,dbcd,dbda,dbdb,dbdc,dbdd,dcaa,dcab,dcac,dcad
,dcba,dcbb,dcbc,dcbd,dcca,dccb,dccc,dccd,dcda,dcdb,dcdc,dcdd,ddaa,ddab,ddac,
ddad,ddba,ddbb,ddbc,ddbd,ddca,ddcb,ddcc,ddcd,ddda,dddb,dddc,dddd}

In[2]:= Head[%[[2]]]

Out[2]= String


E. Martin-Serrano

-----Original Message-----
From: David Park [mailto:djmp at earthlink.net] 
To: mathgroup at smc.vnet.net
Subject: [mg49761] [mg49706] RE: [mg49672] Combinations

Gregory,

Flatten[Outer[List, {a, b, c}, {a, b, c}, {a, b, c}], 2];
Map[SymbolName, %, {2}];
StringJoin@# & /@ %

List["aaa", "aab", "aac", "aba", "abb", "abc", "aca", "acb", "acc", "baa", \
"bab", "bac", "bba", "bbb", "bbc", "bca", "bcb", "bcc", "caa", "cab", "cac",
\
"cba", "cbb", "cbc", "cca", "ccb", "ccc"]

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

From: Gregory Lypny [mailto:gregory.lypny at videotron.ca]
To: mathgroup at smc.vnet.net


Hello everyone,

Does Mathematica have a built-in function that will generate a simple
list all possible combinations of a list of strings?  For example,
{a,b,c}, where the elements are strings, should give 3x3=27 triplets of
"aaa", "aba", etc.

I tried

In[4]:=Outer[{a,b,c},{a,b,c},{a,b,c}]

but I get

{{{a, b, c}[a, a], {a, b, c}[a, b], {
     a, b, c}[a, c]}, {{a, b, c}[b, a], {a, b, c}[b, b], {a, b,
       c}[b, c]}, {{a, b, c}[c, a], {a, b, c}[c, b], {a, b, c}[c, c]}}

which is going to be difficult to match with my data using BinCounts
and the like.

	Regards,

		Greg






  • Prev by Date: Re: ListDensityPlot (solution, and critical comment))
  • Next by Date: Re: Marking a (rectangular) zone in a standard 2D plot using "Rectangle"
  • Previous by thread: Re: Combinations
  • Next by thread: Using "Sum" (i = 1 ... N) in a function definition