MathGroup Archive 2008

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

Search the Archive

Re: Getting rid of those deprecated Do[] loops?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92772] Re: Getting rid of those deprecated Do[] loops?
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 13 Oct 2008 06:16:43 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <gcsctj$8mj$1@smc.vnet.net>

AES wrote:

> Trying to think of a "two-variable iterator" approach that will let one 
> produce the same results as
> 
> data = { };
> Do[
>    Do[
>       data = AppendTo[data, {aa, bb, xx, yy}], 
>       {bb, 10, 30, 10}], 
>    {aa, 1, 2}];  
> Print[data // TableForm];
> 
> without using those universally deprecated (and even worse, nested) 
> Do[ ] loops, not to mention the equally deprecated AppendTo[].
> 
> Somewhat to my surprise, the construction
> 
> Table[{aa = kk[[1]];  bb = kk[[2]];  aa, bb, xx, yy}, 
>           {kk,  {{1, 10},  {1, 20},  {1, 30},  {2, 10},  {2, 20}, 
> {2,30}}}
>           ] // TableForm
> 
> using a two-dimensional iterator iterator variable kk, actually does 
> this, though the "iterator list" is obviously a bit messy.
> 
> Simpler way of defining an iterator to do this?  In more than two 
> iterator dimensions (aa, bb, cc,...)?  (with no functional dependence 
> between the values of aa, bb, cc).
> 
> Is the concept of a multi-dimensional iterator variable kk mentioned in 
> the M6 documentation anywhere?  (A search on "Iterate" or "iteration" 
> brings up 193 hits at 10 per screen.)

(* Compare the order of the iterators in *)

Flatten[
  Table[{aa, bb, xx, yy}, {aa, 1, 2}, {bb, 10, 30, 10}], 1]

{{1, 10, xx, yy}, {1, 20, xx, yy}, {1, 30, xx, yy}, {2, 10,
   xx, yy}, {2, 20, xx, yy}, {2, 30, xx, yy}}

(* versus *)

data = {};
Do[Do[data = AppendTo[data, {aa, bb, xx, yy}], {bb, 10, 30, 10}], {aa,
    1, 2}]
data

{{1, 10, xx, yy}, {1, 20, xx, yy}, {1, 30, xx, yy}, {2, 10,
   xx, yy}, {2, 20, xx, yy}, {2, 30, xx, yy}}

Regards,
-- Jean-Marc


  • Prev by Date: Re: Inconsistent syntax for Row and Column?
  • Next by Date: Re: time based moving average (and other newbie mathematica
  • Previous by thread: Re: Getting rid of those deprecated Do[] loops?
  • Next by thread: Re: Getting rid of those deprecated Do[] loops?