Re: any better to apply a function to second column?

• To: mathgroup at christensen.cybernetics.net
• Subject: [mg525] Re: [mg507] any better to apply a function to second column?
• From: el at qua.crl.melco.co.jp (E. Lange)
• Date: Thu, 9 Mar 95 14:57:13 JST

```> >    I would like to know if there is a better way to
> > do the following.
> >
>
> > 	Suppose I have list of numbers in pairs:
> >
>
> > a = {{x1,y1},{x2,y2},...,{xn,yn}}
> >
>
> > I would like to apply to the sequence {y1,y2,...,yn} the function
> > f so that a new sequence
> >
>
> > b = {{x1,f[y1]},{x2,f[y2]},...,{xn,f[yn]}}
> >
>
> > is generated.
>
>
> Hello Richard,
>
> I think the shortest and clearest method is this:
>
> b = Apply[{#1, f[#2]}&, a, {1}];
>
> > Also, what if I have a matrix of 3 or more columns and I would apply
> > f to only the second column, say. What is a better way in this case?
>
> The Apply method generalizes to an unknown number of additional columns
> like this:
>
> b = Apply[{#1, f[#2], ##3}&, a, {1}];
>

Yet another short and clear method (also works with any number of

b = MapAt[f, #, 2]& /@ a

Compared to

b = Apply[{#1, f[#2], ##3}&, a, {1}]

the MapAt method is about 2.5 times faster, if there are many columns,
and about 10% slower, if there are only a few columns.

Eberhard Lange

```

• Prev by Date: Vote NO!!!!!!!!!
• Next by Date: Extracting Coefficients
• Previous by thread: Re: any better to apply a function to second column?
• Next by thread: Re: any better to apply a function to second column?