Re: Map onto one column of a matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg95566] Re: [mg95485] Map onto one column of a matrix
- From: "David Park" <djmpark at comcast.net>
- Date: Wed, 21 Jan 2009 06:50:06 -0500 (EST)
- References: <30785635.1232448719772.JavaMail.root@m02>
Maybe some expert will know a direct method, but otherwise this is a case
where one could define a useful routine.
MapOnColumn::usage =
"MapOnColumn[f,n][matrix] will Map f onto the elements of column n \
in the matrix.";
SyntaxInformation[MapOnColumn] = {"ArgumentsPattern" -> {_ , _}};
MapOnColumn[f_, n_][matrix_?MatrixQ] /;
1 <= n <= Part[Dimensions[matrix], 2] :=
Transpose[MapAt[f /@ # &, Transpose[matrix], n]]
data = {{a, b}, {c, d}};
data // MapOnColumn[f, 2]
{{a, f[b]}, {c, f[d]}}
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/
From: D. Grady [mailto:D.C.Grady at gmail.com]
Hi, suppose we've got a matrix
data = {{a, b}, {c, d}};
and we want to map a function onto just the second column, so we want
to end up with
{{a, f[b]}, {c, f[d]}}.
The best way I've found to do this is with MapAt:
MapAt[f, data, Table[{i, 2}, {i, Length@data}]]
Is there a better way? I keep hoping there's some notation similar to
what's used in Part that lets you refer to a whole column with All,
like data[[All,2]]. Thanks!
-Daniel