MathGroup Archive 1999

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

Search the Archive

RE: Combining Lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16783] RE: [mg16722] Combining Lists
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Wed, 24 Mar 1999 17:07:10 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

	JOHN C ERB  wrote:
--------------------------------------
I have a set of x & y data points in one list;
xydata={{x1,y1},{x2,y2},{x3,y3},{x4,y4}};
and a corresponding set of z data points in another list:
zdata={z1,z2,z3,z4};

I wish to combine them into one list of {x,y,z} data points,
which I can do by:

{temp1,temp2}=Transpose[xydata];
xyzdata=Transpose[{temp1,temp2,zdata}]

which gives the desired result:
{{x1,y1,z1},{x2,y2,z2},{x3,y3,z3},{x4,y4,z4}}

QUESTION:  Is there a more efficient way of doing this?
------------------------

You can get rid of the temp variables using the line below.  You could also
use Thread instead of Transpose.  Some have claimed Thread is faster than
Transpose, but my timing tests of this claim aren't conclusive.  Thread may
be faster in some cases, but I think it depends on the dimensions of the
matrix.
 
 
In[5]:=
Transpose[Join[Transpose[xydata],{zdata}]]
Out[5]=
{{x1,y1,z1},{x2,y2,z2},{x3,y3,z3},{x4,y4,z4}}
 
 
In[6]:=
Thread[Join[Thread[xydata],{zdata}]]
Out[6]=
{{x1,y1,z1},{x2,y2,z2},{x3,y3,z3},{x4,y4,z4}}

 
Regards,
Ted Ersek


  • Prev by Date: Re: Matlab -> Mathematica integration question?
  • Next by Date: Re: Help Defining Variables (with long names)
  • Previous by thread: Re: Combining Lists
  • Next by thread: Re: Combining Lists