Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7758 > unrolled thread
| Started by | hanciong awesome <hanciong@gmail.com> |
|---|---|
| First post | 2012-01-11 22:21 +0000 |
| Last post | 2012-01-12 09:20 +0000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
take square of the second and third column of a table hanciong awesome <hanciong@gmail.com> - 2012-01-11 22:21 +0000
Re: take square of the second and third column of a table "Nasser M. Abbasi" <nma@12000.org> - 2012-01-12 09:20 +0000
Re: take square of the second and third column of a table "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2012-01-12 09:22 +0000
Re: take square of the second and third column of a table "Nasser M. Abbasi" <nma@12000.org> - 2012-01-12 09:23 +0000
Re: take square of the second and third column of a table Helen Read <readhpr@gmail.com> - 2012-01-12 09:20 +0000
| From | hanciong awesome <hanciong@gmail.com> |
|---|---|
| Date | 2012-01-11 22:21 +0000 |
| Subject | take square of the second and third column of a table |
| Message-ID | <jel20o$lkl$1@smc.vnet.net> |
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
[toc] | [next] | [standalone]
| From | "Nasser M. Abbasi" <nma@12000.org> |
|---|---|
| Date | 2012-01-12 09:20 +0000 |
| Message-ID | <jem8ki$1ak$1@smc.vnet.net> |
| In reply to | #7758 |
On 1/11/2012 4:21 PM, hanciong awesome wrote:
>
> how can I take square of the 2nd and 3rd column?
I forgot to mention the simplest method, which is to square the slice
of the matrix directly.
tbl = {{1, 2, 3},{4, 5, 6},{7, 8, 9},{2, 3, 4}}
for second col:
tbl[[All, 2]]^2
-->{4, 25, 64, 9}
to do both cols at same time:
tbl[[All, {2, 3}]]^2
---> {{4, 9}, {25, 36}, {64, 81}, {9, 16}}
I like to Map allot, and so sometimes I overlook that it might not
be needed.
--Nasser
[toc] | [prev] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2012-01-12 09:22 +0000 |
| Message-ID | <jem8pd$1ch$1@smc.vnet.net> |
| In reply to | #7758 |
On Wed, 11 Jan 2012 22:21:12 -0000, hanciong awesome <hanciong@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
[toc] | [prev] | [next] | [standalone]
| From | "Nasser M. Abbasi" <nma@12000.org> |
|---|---|
| Date | 2012-01-12 09:23 +0000 |
| Message-ID | <jem8qc$1ck$1@smc.vnet.net> |
| In reply to | #7758 |
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
[toc] | [prev] | [next] | [standalone]
| From | Helen Read <readhpr@gmail.com> |
|---|---|
| Date | 2012-01-12 09:20 +0000 |
| Message-ID | <jem8lh$1b9$1@smc.vnet.net> |
| In reply to | #7758 |
On 1/11/2012 5: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
>
Well, Mathematica knows the length of your table.
table = RandomInteger[{-10, 10}, {50, 3}]
Table[{table[[n]][[1]], table[[n]][[2]]^2, table[[n]][[3]]^2}, {n, 1,
Length[table]}]
That said, Map would be a lot easier. Make a little function for what
you want to do to one row, and Map it across your table, like so.
f[{x_, y_, z_}] := {x, y^2, z^2}
Map[f, table]
Even quicker, once you get the hang of working with pure functions:
Map[{#[[1]], #[[2]]^2, #[[3]]^2} &, table]
--
Helen Read
University of Vermont
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web