Re: Trouble with tables
- To: mathgroup at smc.vnet.net
- Subject: [mg64826] Re: Trouble with tables
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 5 Mar 2006 03:18:44 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <dubjlr$gua$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Luiz Melo wrote:
> Hi there,
> I'm having a little trouble with the function "Table". Using:
>
> t1 = Table[{x[1, 1], x[1, 2], y}, {x[1, 1], 1, 3}, {x[1, 2], 1, 3}]
>
> the variables x[1,1] and x[1,2] assume the values 1, 2 and 3, that's fine.
> Now I want to automatically generate the argument of the function Table above
> and then evaluate it. I tried the following program:
>
> ni = 1; nj = 2;
> a = Flatten[Table[x[i, j], {i, 1, ni}, {j, 1, nj}]]; d = Dimensions[a][[1]];
> b = {}; Do[b = Append[b, {a[[i]], 1, 3}], {i, 1, d}];
> t2 = Table[Join[{Append[a, y]}, b]];
>
> and I got a result (t2) which is different from t1. Why?
>
> Thanks,
> Luiz
>
>
>
Hi Luiz,
This will do what you want (either In[7] or In[8]):
In[1]:=
t1 = Table[{x[1, 1], x[1, 2], y}, {x[1, 1], 1, 3}, {x[1, 2], 1, 3}]
Out[1]=
{{{1, 1, y}, {1, 2, y}, {1, 3, y}}, {{2, 1, y}, {2, 2, y}, {2, 3, y}},
{{3, 1, y}, {3, 2, y}, {3, 3, y}}}
In[2]:=
ni = 1; nj = 2;
a = Flatten[Table[x[i, j], {i, 1, ni}, {j, 1, nj}]];
d = Dimensions[a][[1]];
b = {}; Do[b = Append[b, {a[[i]], 1, 3}], {i, 1, d}];
args = Join[{Append[a, y]}, b];
In[7]:=
t2 = Table[args[[1]], Evaluate[args[[2]]], Evaluate[args[[3]]]]
Out[7]=
{{{1, 1, y}, {1, 2, y}, {1, 3, y}}, {{2, 1, y}, {2, 2, y}, {2, 3, y}},
{{3, 1, y}, {3, 2, y}, {3, 3, y}}}
In[8]:=
t3 = Table[Append[a, y], Evaluate[b[[1]]], Evaluate[b[[2]]]]
Out[8]=
{{{1, 1, y}, {1, 2, y}, {1, 3, y}}, {{2, 1, y}, {2, 2, y}, {2, 3, y}},
{{3, 1, y}, {3, 2, y}, {3, 3, y}}}
In[9]:=
{t3 == t1, t2 == t1}
Out[9]=
{True, True}
Best regards,
/J.M.