19

I have read x-data (from text files) into list1, and y-data similarly into list2:

list1 = { 0.0,    0.172,  0.266, ..}
list2 = {-5.605, -5.970, -6.505, ..} 

How do I combine the two lists in order to plot points {0.0, -5.605}, {0.172, -5.970}, {0.266, -6.505},....

user2375856
  • 191
  • 1
  • 1
  • 3
  • 1
    maybe you want to try: `Riffle[list1, list2]~Partition~2` – Pinguin Dirk May 13 '13 at 07:21
  • 1
    the user deserves an explanation why this is closed. It is most certainly not "off topic". – agentp May 13 '13 at 11:54
  • 1
    To closers: Your opinion about a language (Mathematica (TM))you don't know at all isn't welcomed. You should use your close powers in a wiser way. – Dr. belisarius May 13 '13 at 14:17
  • possible duplicate of [ListPlot With Two Data Sets in Mathematica](http://stackoverflow.com/questions/2602991/listplot-with-two-data-sets-in-mathematica) – Mr.Wizard May 14 '13 at 12:07
  • @belisarius I like to think I know a thing or two about the language and I'm still voting to close. ;-p (note the wink) – Mr.Wizard May 14 '13 at 12:08
  • @Mr.Wizard My rage was about the OT reason, not the closing per se. I think closing a questions requires _at least_ a responsible behavior. But I guess you already know that – Dr. belisarius May 14 '13 at 12:15

4 Answers4

20

If you don't like Pinguin Dirk's suggestion try

Transpose[{list1,list2}]
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
8

yet another..

MapThread[ {#1, #2} & , {list1, list2}]

or if you want to gracefully handle unequal length lists:

MapThread[ {#1, #2} &, Take[#, All, Min @@ Length /@ #] &@{list1, list2} ]
agentp
  • 6,849
  • 2
  • 19
  • 37
2

Here is another answer that creates a reusable function to pair up two vectors. The function uses a pure function that maps over the length of the shortest vector to create the pairs.

    list1 = {1, 2, 3, 4, 5}; 
    list2 = {13, 18, 20, 18, 13};
    pairUp[xValues_ , yValues_] := ({xValues[[#]], yValues[[#]]}) & /@ 
       Range[Min[Length[xValues], Length[yValues]]];

    pairUp[list1, list2]
    (*
      {{1, 13}, {2, 18}, {3, 20}, {4, 18}, {5, 13}}
    *)

Hope this helps,

Edmund

PS: New to Mathematica and hoping to improve my understanding by trying to answer a few questions on here from time to time.

Edmund
  • 488
  • 4
  • 21
0

Here you go

Partition[Riffle[x,y],2]
T. H
  • 509
  • 4
  • 3