Re: loops?

*To*: mathgroup at smc.vnet.net*Subject*: [mg92814] Re: loops?*From*: David Bailey <dave at Remove_Thisdbailey.co.uk>*Date*: Tue, 14 Oct 2008 04:56:02 -0400 (EDT)*References*: <gcv784$dsn$1@smc.vnet.net>

Bob Hanlon wrote: > n = 100; m = 100; > > data1 = {}; > Timing[Do[ > Do[data1 = AppendTo[data1, {aa, bb, xx, yy}], {bb, 10, m, > 10}], {aa, 1, n}];] > > {0.03825,Null} > > Timing[data2 = > Table[{aa = kk[[1]]; bb = kk[[2]]; aa, bb, xx, yy}, {kk, > Flatten[Outer[{#1, #2} &, Range[n], Range[10, m, 10]], 1]}];] > > {0.003871,Null} > > Timing[data3 = > Flatten[Outer[{#1, #2, xx, yy} &, Range[n], Range[10, m, 10]], > 1];] > > {0.00092,Null} > > data1 == data2 == data3 > > True > > > Bob Hanlon > > ---- AES <siegman at stanford.edu> 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.) > > > -- > > Bob Hanlon > > A quick comment on the above - never time any process for less than (at least) a few seconds - otherwise the result is meaningless. This is not aimed at you, Bob, but at the original author of the question. David Bailey http://www.dbaileyconsultancy.co.uk