2

I am a beginner in Mathematica. My question is: I have huge amount of x,y and z co-ordinates in separate lists named XCORD,YCORD,ZCORD and I want to combine them in one list

Example:
If the x co-oridinates list is given by XCORD = {x1,x2,x3}, the y co-ordinates list byYCORD = {y1,y2,y3} and the z co-ordinates list by ZCORD = {z1,z2,z3}, I would like to have a resulting list of co-ordinates that looks like this:

 {{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}}
Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
fazil
  • 47
  • 1
  • 2
  • 2
    Welcome to StackOverflow Faz! Please note that I edited your question somewhat and removed your desperate plea. Such utterances are unnecessary here to get attention and help. Please vote-up answers you like using the vote buttons, and don't forget to accept the answer you like as the final answer using the checkmark button. You may want to wait a little for more answers to come in before doing that. – Sjoerd C. de Vries Aug 15 '11 at 14:01

2 Answers2

7

You can do this with Transpose:

XCORD = {x1, x2, x3};
YCORD = {y1, y2, y3};
ZCORD = {z1, z2, z3};

res = Transpose[{XCORD, YCORD, ZCORD}]

==> {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}

More information about working with lists can be found here, and more specific to your question, here.

Please note that it's better (though it's allowed) not to start any variable of your own with a uppercase letter. Using a lowercase start of your variable name means you'll never get into conflict with the thousands of built-in symbols that all start with an uppercase letter.

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • 1
    @acl I was cheating. With Mr.Wizard back, I thought I might run my WatchSO utility (http://stackoverflow.com/q/6505675/615464) and it helps. I had even time editing the question before working on the answer. – Sjoerd C. de Vries Aug 15 '11 at 14:14
  • @Sjoerd I started flagging Mr. Wiz answers for infringing the speed limit. Hopefully he will be banned if they can catch him :) – Dr. belisarius Aug 15 '11 at 20:33
  • @belisarius LOL, but it seems he's a bit out of shape lately. – Sjoerd C. de Vries Aug 15 '11 at 20:38
  • @Sjoerd, yeah. I beat him to the punch earlier. Must be the "vacation;" he needs time to bring himself back up to cruising speed. – rcollyer Aug 16 '11 at 01:55
3

Alternate solution using the MapThread function:

In[2]:= MapThread[List, {{x1, x2, x3}, {y1, y2, y3}, {z1, z2, z3}}]

Out[2]= {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}

For large lists, Transpose is an ordner of a magnitude faster, though:

In[3]:= With[{n=10^6}, x=RandomReal[1, n]; y=RandomReal[2, n]; z=RandomReal[3, n];]

In[4]:= Transpose[{x, y, z}]; // Timing

Out[4]= {0.644832, Null}

In[5]:= MapThread[List, {x, y, z}]; // Timing

Out[5]= {5.87969, Null}
sakra
  • 62,199
  • 16
  • 168
  • 151
  • I would definitely encourage people to become more familiar with `MapThread`, it is extremely useful in general. On the other hand, which is faster in this case: `Transpose` or `MapThread`? – rcollyer Aug 15 '11 at 15:51
  • thank you. I appreciate the timings, especially when there are competing options, but I was being a little facetious. – rcollyer Aug 16 '11 at 01:56