Re: building a list containing elements f(i,j)
- To: mathgroup at smc.vnet.net
- Subject: [mg71063] Re: building a list containing elements f(i,j)
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 8 Nov 2006 06:01:59 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <eimr3e$dhv$1@smc.vnet.net>
xarnaudx at gmail.com wrote: > hi, > i want to construct a list of the form: > { f[1,1], ... f[n,m] } > So the list contains elements of the form f[i,j] with i ranging from 0 > to n, and j from 0 to somebound[i]. > > What would be the most elegant way to this? > thanks > You can use either *Table* or *Array*, then *Flatten* the resulting 2D-list to make it one dimensional. Beware that Mathematica use the one -- and not zero -- to index lists or arrays. You will find hereafter several example of list construction, some of them with the second index depending on the value of the first one. In[1]:= l1 = Flatten[Table[f[i, j], {i, 0, 3}, {j, 0, 2}]] Out[1]= {f[0, 0], f[0, 1], f[0, 2], f[1, 0], f[1, 1], f[1, 2], f[2, 0], f[2, 1], f[2, 2], f[3, 0], f[3, 1], f[3, 2]} In[2]:= l2 = Flatten[Array[f, {4, 3}, {0, 0}]] Out[2]= {f[0, 0], f[0, 1], f[0, 2], f[1, 0], f[1, 1], f[1, 2], f[2, 0], f[2, 1], f[2, 2], f[3, 0], f[3, 1], f[3, 2]} In[3]:= l1 == l2 Out[3]= True In[4]:= Table[f[i, j], {i, 0, 3}, {j, 0, i/2}] Out[4]= {{f[0, 0]}, {f[1, 0]}, {f[2, 0], f[2, 1]}, {f[3, 0], f[3, 1]}} In[5]:= somebound[i_] := {4, 1, 3, 2}[[i + 1]]; Table[f[i, j], {i, 0, 3}, {j, 0, somebound[i]}] Out[6]= {{f[0, 0], f[0, 1], f[0, 2], f[0, 3], f[0, 4]}, {f[1, 0], f[1, 1]}, {f[2, 0], f[2, 1], f[2, 2], f[2, 3]}, {f[3, 0], f[3, 1], f[3, 2]}} HTH, Jean-Marc