MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Map onto a column

  • To: mathgroup at smc.vnet.net
  • Subject: [mg125458] Re: Map onto a column
  • From: roby <roby.nowak at gmail.com>
  • Date: Wed, 14 Mar 2012 00:43:12 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jjmv20$cfr$1@smc.vnet.net>

On 13 Mrz., 09:04, Sly Pandemia <slypande... at gmail.com> wrote:
> Is there a neat way to map a function onto a specific column in an
> array?
>
> For example if I have
>
> a = {{1,2,3},{4,5,6},{7,8,9}}
>
> and I want to map f onto the second element in each row. I would
> normally use
>
> b = Map[MapAt[f,#,2]&,a]
>
> but this seems a bit awkward. I would expect to be able to use
>
> b = MapAt[f,a,{All,2}]
>
> but this appears to be illegal syntax.
>
> Is there any way to do what I want with a single Map/MapAt function?

... well a single MapAt[ ] but at the cost of two Transpose[ ]
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} //
   Transpose // MapAt[f /@ # &, #, 2] & // Transpose

or a single MapIndexed[ ] but some matching
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} //
 MapIndexed[If[MatchQ[#2, {_, 2}], f@#, #] &, #, {2}] &

but all in all not better than your solution

my best effort:
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} //
 ReplacePart[#, {v_, 2} :> f@#[[v, 2]]] &

Robert



  • Prev by Date: Mathematica and square roots
  • Next by Date: Message using FindFit with LevenbergMarquardt Method
  • Previous by thread: Re: Map onto a column
  • Next by thread: Re: Map onto a column