Re: take square of the second and third column of a table
- To: mathgroup at smc.vnet.net
- Subject: [mg124194] Re: take square of the second and third column of a table
- From: "Nasser M. Abbasi" <nma at 12000.org>
- Date: Thu, 12 Jan 2012 04:21:03 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jel20o$lkl$1@smc.vnet.net>
- Reply-to: nma at 12000.org
On 1/11/2012 4:21 PM, hanciong awesome 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
>
given
tbl = {{1, 2, 3},{4, 5, 6}, {7, 8, 9} ,{2, 3, 4}}
one way for the second column:
Map[#[[2]]^2 &, tbl]
-->{4, 25, 576}
and for the third column
Map[#[[3]]^2 &, tbl]
-->{9, 36, 1296}
or you can make a list of the columns you want to square and
do both at the same time, like this
lstOfColumns = {2, 3};
Map[tbl[[All, #]]^2 &, lstOfColumns]
-->{{4, 25, 576}, {9, 36, 1296}}
and many other ways.
--Nasser