Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: [mg5389] Q:Nicer than: Function[x,MapAt[Im,x,2]]/@data
Replies: 0

 Allan Hayes Posts: 1,508 Registered: 12/6/04
[mg5389] Q:Nicer than: Function[x,MapAt[Im,x,2]]/@data
Posted: Dec 6, 1996 11:48 PM

rommel@bc.edu
[mg5389] Q:Nicer than: Function[x,MapAt[Im,x,2]]/@data
writes
>>>>>>
I want to ListPlot the imaginary part of data:

data={{x0,z0},{x1,z1},{x2,z2}}

The x? are real, the z? complex.

My simple but ugly solution was

Transpose[{Transpose[data][[1]],Im[Transpose[data][[2]]]}]

after reading a little I came to the shorter

Function[x,MapAt[Im,x,2]]/@data

Is there a nicer way to do it?
<<<<<<

Some Experiments:

lst = Table[{Random[],Random[]+I Random[]},{2000}];

{1.25 Second, Null}

MapAt[Im,#,2]&/@lst;//Timing (*faster function*)
{0.7 Second, Null}

Apply[{#,Im[#2]}&,lst,1];//Timing (*use Apply*)
{0.466667 Second, Null}

lst/.Complex[x_,y_]:>y;//Timing (*pattern matching*)
{0.483333 Second, Null}

lst/.{a_,Complex[x_,y_]}:>{a,y};//Timing (*help matching*)
{0.383333 Second, Null}

{0.3 Second, Null}

than Transpose*)
{0.25 Second, Null}

Allan Hayes
hay@haystack.demon.co.uk
http://www.haystack.demon.co.uk