MathGroup Archive 2000

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

Search the Archive

Re: generating all combinations.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22400] Re: [mg22395] generating all combinations.
  • From: BobHanlon at aol.com
  • Date: Wed, 1 Mar 2000 00:39:49 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

allcomb = Table[{i, j, k}, {i, -2, 2}, {j, -2, 2}, {k, -2, 2}];

Your fltcomb can be written more succinctly as

fltcomb = Flatten[allcomb, 2];

A more generalized approach is

combtn[nbrDigits_Integer, rangeLow_Integer:1, rangeHi_Integer] := 
  Flatten[Outer[List, 
      Sequence @@ Table[Range[rangeLow, rangeHi], 
          {nbrDigits}]], 2]

fltcomb == combtn[3, -2, 2]

True

Bob Hanlon

In a message dated 2/27/2000 8:07:42 PM, d8442803 at student.nsysu.edu.tw writes:

>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.
>


  • Prev by Date: Rearranging a tensor
  • Next by Date: newbie, problems with loading packages
  • Previous by thread: Re: generating all combinations.
  • Next by thread: Re: generating all combinations.