Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1995
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1995

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

Search the Archive

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

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg516] Re: [mg507] any better to apply a function to second column?
  • From: Richard Mercer <richard at seuss.math.wright.edu>
  • Date: Tue, 07 Mar 1995 09:21:47 -0500

>  Hi,
>  	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.
>  

>  My current solution is
>  

>  b = Transpose[{First /@ a,f[Last /@ a]}]
>  

>  However, I suspect there should be a much more simpler
>  way than this.
>  

>  Does any body know a more straight forward way to do
>  this?
>  

>  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?
>  

>  Thanks for any info
>  

>  Richard

This is a common and important situation.
The "best" solution? Here are two candidates.

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

a /. {x_,y_}:>{x,f[y]}

I tend to prefer the first, because the second has the potential to make  
unwanted changes at deeper levels (should they exist). This possibility could  
be avoided with

Replace[#,{x_,y_}:>{x,f[y]}]& /@ a

Richard Mercer


  • Prev by Date: Re: auto notebook running
  • Next by Date: Graphics on a NeXT into MSWord on a PC?
  • 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?