MathGroup Archive 2000

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

Search the Archive

Re: generating all combinations.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22410] Re: generating all combinations.
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 1 Mar 2000 00:39:59 -0500 (EST)
  • References: <89cea2$bsp@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Wen-Feng:


allcombinations[lst_List, lng_Integer?NonNegative] :=
  Flatten[Outer[List, Sequence @@ Table[lst, {lng}]], lng - 1]

For your eample:

allcombinations[Range[-2, 2], 3]

Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565



"Wen-Feng Hsiao" <d8442803 at student.nsysu.edu.tw> wrote in message
news:89cea2$bsp at smc.vnet.net...
> Dear all,
>
> I would like to generate all the combinations n-digit lists, which can
> vary between a certain range and duplications are allowed. The three
> digits with range between -2 to 2 is the following:
>
> allcomb =
>     Table[{i, j, k}, {i, -2, 2}, {j, -2, 2}, {k, -2, 2}];
> fltcomb = Partition[Flatten[allcomb], 3];
>
> Is there a more succinct way to do that? If I would like to expand the
> code to 10 digits, the code would looks clumsy.
>
> Thanks for your help.
>
> Wen-Feng
>
>



  • Prev by Date: Notation for atomic objects
  • Next by Date: Re: ListPlot with missing values
  • Previous by thread: Re: generating all combinations.
  • Next by thread: Re: generating all combinations.