Re: Variable-length list?
- To: mathgroup at smc.vnet.net
- Subject: [mg83494] Re: Variable-length list?
- From: "Szabolcs HorvÃt" <szhorvat at gmail.com>
- Date: Wed, 21 Nov 2007 03:04:05 -0500 (EST)
- References: <fhu73f$71m$1@smc.vnet.net> <4742F526.5020201@gmail.com>
On Nov 21, 2007 6:51 AM, Szabolcs Horvát <szhorvat at gmail.com> wrote: > On Nov 21, 2007 3:32 AM, Hoa Bui <hoabui05 at gmail.com> wrote: > > Hi, > > > > Thanks for your comment, but this is not quite what I wanted to do. > > I tried to simplify the problem, but basically if I have a vector > > v={v[[1]],v[[2]],..v[[n]]} > > and each element v[[i]] has a range in which it can vary. I want to be > > able to search over all the possible combinations of the v[[i]]. (Each > > combination will return a vector, lets call it vi, and I want to use > > that vi to do something else.) > > The problem is that the number of dimensions (n) is not a fixed > > number, I want it to be a variable as well. > > > > Here's an example: > > If n=2, then v={a,b}. Say a={a1,a2} and b={b1,b2,b3}, then the combinations are: > > vi={{a1,b1}, {a1,b2}, {a1,b3}, {a2,b1}, {a2,b2}, {a2,b3}} > > > > If n=3, then v={a,b,c}. Say a={a1,a2}; b={b1,b2,b3}; c={c1,c2}, then I > > want to get: > > vi={{a1,b1,c1}, {a1,b2,c1}, {a1,b3,c1}, {a2,b1,c1}, {a2,b2,c1}, > > {a2,b3,c1}, {a1,b1,c2}, {a1,b2,c2}, {a1,b3,c2}, {a2,b1,c2}, > > {a2,b2,c2}, {a2,b3,c2}} > > > > If n=4, then I'll need another list d to represent my 4th dimension. > > Take a look at Outer[] > Hmm ... Actually Tuples[] already does this: In[1]:= Tuples[{{a1, a2}, {b1, b2, b3}, {c1, c2}}] Out[1]= {{a1, b1, c1}, {a1, b1, c2}, {a1, b2, c1}, {a1, b2, c2}, {a1, b3, c1}, {a1, b3, c2}, {a2, b1, c1}, {a2, b1, c2}, {a2, b2, c1}, {a2, b2, c2}, {a2, b3, c1}, {a2, b3, c2}} Szabolcs