Re: Map onto one column of a matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg95558] Re: Map onto one column of a matrix
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 21 Jan 2009 06:48:37 -0500 (EST)
On 1/20/09 at 5:44 AM, D.C.Grady at gmail.com (D. Grady) wrote:
>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?
There are a variety of other ways. These include:
f[Last@#]&/@data (* works since 2nd column is the last column *)
data/.{a_,b_}->{a,f[b]} (* for two column array *)
data/.{a_,b_,c__}->{a,f[b],c} (* for n column array *)
Or if you like the data[[All,1]] construct and f has the
attribute Listable then
Transpose@{data[[All,1]],f[data[[All,2]]]}