MathGroup Archive 2004

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

Search the Archive

Creating combinations from a group of sets, PART 2

  • To: mathgroup at smc.vnet.net
  • Subject: [mg48739] Creating combinations from a group of sets, PART 2
  • From: John Kiehl <john.kiehl at soundtrackny.com>
  • Date: Fri, 11 Jun 2004 23:59:11 -0400 (EDT)
  • Reply-to: John Kiehl <john.kiehl at soundtrackny.com>
  • Sender: owner-wri-mathgroup at wolfram.com

the recent msg[48679] on combining elements from a group of sets has reminded me that I always wanted a Table[] command that would step through lists instead of using iterators.  For example, my NewTable[ ] command would be used something like this:

NewTable[ (i+j)/(m+n),{1,2,3},{5,10.15.20},{3.14,1,414,0.707},{faith,hope,charity}]

Somehow, magically, the first list would be associated with "i", the second list with "j", etc...

Of course, all this could be accomplished, by this Mathematica code:

a={1,2,3};
b={5,10.15.20};
c={3.14,1,414,0.707};
d={faith,hope,charity};

Table[ ( a[[i]] + b[[j]] ) / ( c[[m]] + d[[m]] ),{i,1,Lengh[a]},{j,1,Length[b]},{m,1,Length[c]},{n,1,Length[d]}]

but that's a lot of typing.
-------------------------------------------------------------------------------------
So here's my first attempt at a NewTable[] command using Distribute[] as suggested in responses to msg[48679].  It assumes that the first expression in NewTable[ ] is a pure function.

Clear[a,b,c,d,e,f,g];
NewTable[f_,h___]:=f  /@ Distribute[{h},List]

and here it is in use:

In[]:=table[#[[1]] + #[[2]]^#[[3]] & , {a, b, c}, {d, e}, {f, g}]
Out[]:={a + d^f, a + d^g, a + e^f, a + e^g, b + d^f, b + d^g, b + e^f, b + e^g, c + d^f, c + d^g, c + e^f, c + e^g}


This has the nice advantage of working with any number of lists.  I just wish I could get rid of all the double bracket typing!

-----------------------------------------------------------------------------------------
I'm not sure I can even figure out how WRI has programmed the regular Table[] command.  What would the code for Table[] look like?

john kiehl



  • Prev by Date: Re: Merging lists if an element in each partially matches?
  • Next by Date: Re: Merging lists if an element in each partially matches?
  • Previous by thread: Re: (**) comments highlighted - newbie
  • Next by thread: Re: Creating combinations from a group of sets, PART 2