Re: N-nested For loops, with N variable - Best way to implement?
- To: mathgroup at smc.vnet.net
- Subject: [mg60770] Re: [mg60743] N-nested For loops, with N variable - Best way to implement?
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Wed, 28 Sep 2005 01:41:06 -0400 (EDT)
- References: <200509270745.DAA18921@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Sep 27, 2005, at 3:45 AM, lost.and.lonely.physicist at gmail.com wrote:
> Hello everyone
>
> I'd like to seek some advice regarding how to best implement N nested
> For loops such that
>
> (1) N is a variable - i.e. I tell Mathematica what N is and it does
> the
> For loop N times.
>
> (2) For each loop I want to do something like For[a_k=1,a_k<=A,a++,
> ...], k = 1,...,N and I want to use the all the running values
> {a_1,...,a_N} to do stuff within the innermost loop.
>
> Maybe there's even a way to do this w/o using For loops? Table maybe?
I usually use the following construct in this situation; supposing
ends is a list of the last value for each a_k (ends={A_1,A_2,...A_k})
then
Block[{a,lims},lims=Table[{a[i],1,ends[[i]]},{i,k}];Table[(*Your
operations here*),Evaluate[Sequence@@lims]]
is equivalent to k nested for loops with a[1], a[2],...a[k] being the
loop variables.
Regards,
Ssezi
- References:
- N-nested For loops, with N variable - Best way to implement?
- From: lost.and.lonely.physicist@gmail.com
- N-nested For loops, with N variable - Best way to implement?