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