Re: take square of the second and third column of a table
- To: mathgroup at smc.vnet.net
- Subject: [mg124193] Re: take square of the second and third column of a table
- From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
- Date: Thu, 12 Jan 2012 04:20:43 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jel20o$lkl$1@smc.vnet.net>
On Wed, 11 Jan 2012 22:21:12 -0000, hanciong awesome <hanciong at gmail.com>
wrote:
> Hello, suppose I have a very long table like this:
>
> 1 2 3
> 4 5 6
> 7 8 9
> .......
> 2 3 4
>
> how can I take square of the 2nd and 3rd column? I always do it by
> making a new table. so let's say the above table is A and it has 100
> lines, then I make the new table as the following:
>
> B=Table[{A[[n]][[1]],A[[n]][[2]]^2,A[[n]][[3]]^2},{n,1,100}]
>
> But if the length of the table is unknown, this way is impractical.
> Could anyone suggests better way? thank you
>
You could try
In[1] :=
table = Partition[Range[18], 3];
In[2] :=
MapThread[{#1, #2^2, #3^2} &, Transpose[table]] // TableForm
Out[2]//TableForm =
1 4 9
4 25 36
7 64 81
10 121 144
13 196 225
16 289 324
or perhaps
In[3] :=
Transpose@MapAt[#^2 &, Transpose[table], {{2}, {3}}] // TableForm
Out[3]//TableForm =
1 4 9
4 25 36
7 64 81
10 121 144
13 196 225
16 289 324
or even
In[4] :=
Block[{table = table},
table[[All, {2, 3}]] = table[[All, {2, 3}]]^2;
table
] // TableForm
Out[4]//TableForm =
1 4 9
4 25 36
7 64 81
10 121 144
13 196 225
16 289 324