MathGroup Archive 2007

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

Search the Archive

Re: Variable-length list?

  • To: mathgroup at
  • Subject: [mg83494] Re: Variable-length list?
  • From: "Szabolcs HorvÃt" <szhorvat at>
  • Date: Wed, 21 Nov 2007 03:04:05 -0500 (EST)
  • References: <fhu73f$71m$> <>

On Nov 21, 2007 6:51 AM, Szabolcs Horvát <szhorvat at> wrote:
> On Nov 21, 2007 3:32 AM, Hoa Bui <hoabui05 at> 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}}


  • Prev by Date: Re: addons in Mathematica 6???
  • Next by Date: Re: Scalars Instead of Lists with One Element
  • Previous by thread: Re: Variable-length list?
  • Next by thread: Re: Variable-length list?