MathGroup Archive 2007

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

Search the Archive

Re: Variable-length list?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83486] Re: Variable-length list?
  • From: "Hoa Bui" <hoabui05 at gmail.com>
  • Date: Wed, 21 Nov 2007 02:59:51 -0500 (EST)
  • References: <fhu73f$71m$1@smc.vnet.net> <4742F526.5020201@gmail.com>

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

You see, it would be easy if n is a fixed number (because then I can
just use Array[] with the specified dimensions {na, nb, nc,...}. But
the trouble is, even the number n of dimensions is a variable.
Obviously, I cannot use For[], because each For[] only solves 1
dimension so I can only have a fixed number of dimensions searched.

Is this doable?

Thank you, and Happy Thanksgiving!
HB



On 11/20/07, Szabolcs Horv=E1t <szhorvat at gmail.com> wrote:
> Hoa Bui wrote:
> > Hi all,
> >
> > I want to have either a module or a function that inputs a
> > varying-length list M[[nx]], where nx is a parameter, and accesses all
> > of its elements.
> >
> > For example:
> >
> > f[M_?(Function[v,VectorQ[v,IntegerQ]]]:=Module[{i,j,k},
> > ....
> > ]
> >
> > If M={3,4} then f would do something like:
> > For [i=1,i<3,i++,
> > For [j=1,j<4,j++,
> > Print[i*j];
> > ]
> > ]
> >
> > If M={3,4,5} then:
> > For [i=1,i<3,i++,
> > For [j=1,j<4,j++,
> > For [k=1,k<5,j++,
> > Print[i*j*k];
> > ]
> > ]
> > ]
> > ]
> >
> >
> > I wonder if this is doable in Mathematica, and/or what would be a way t=
o do it?
> >
>
> Hi,
>
> Never use For[], and don't use Print[] for displaying the result---just
> return it instead.
>
> f[{dd__Integer}] := Array[Times, {dd}]
>
> You can still Flatten[] and Print[] the result if this is necessary.
>
> --
> Szabolcs
>


  • Prev by Date: Re: Variable-length list?
  • Next by Date: Re: Get list of function variables?
  • Previous by thread: Re: Variable-length list?
  • Next by thread: Using FindRoot