MathGroup Archive 2003

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

Search the Archive

RE: proper way to manipulate lists?

  • To: mathgroup at
  • Subject: [mg40197] RE: [mg40182] proper way to manipulate lists?
  • From: "Wolf, Hartmut" <Hartmut.Wolf at>
  • Date: Tue, 25 Mar 2003 14:49:07 -0500 (EST)
  • Sender: owner-wri-mathgroup at

>-----Original Message-----
>From: Nathan Moore [mailto:nmoore at]
To: mathgroup at
>Sent: Tuesday, March 25, 2003 9:04 AM
>To: mathgroup at
>Subject: [mg40197] [mg40182] proper way to manipulate lists?
>I'm sure this is a one-liner, but it has stumped me for years.  Suppose
>I'm working with a 2-d array of data,
>	data = {{x1,y1},{x2,y2}...{xN,yN}};
>and I want to transform the data according to,
>	data2 = {{x1*a,y1*b},{x2*a,y2*b}...{xN*a,yN*b}};
>The only way I know to do this is to move the data to excel,
>performing the transform and then move the data back to 
>Mathematica.  This
>is crazy - there must be a better way - is there?  Please tell me there
>Nathan Moore, University of Minnesota Physics Department


several possibilities, some given here

(1)	{a, b}*# & /@ data

(2)	Table[{a, b}, {Length[data]}]*data

(3)	Thread[Unevaluated[{a, b}*#], List, -1] &[data]

(4)	Transpose[{a, b}*Transpose[data]]

Number (1) perhaps comes to mind as first, simple idea (and of course is
quite ok); (2) appears to be doubtful at first sight, yet performs better
than (1) on non-packed data. I would have bet on (3) but (4) performs even

For packed data (3) is roughly off, (1) comes slightly better than (4),
clearly best now is (2).

Hartmut Wolf

  • Prev by Date: Re: Ellipse Drawing
  • Next by Date: Re: Cell grouping rules
  • Previous by thread: Re: proper way to manipulate lists?
  • Next by thread: Re: proper way to manipulate lists?