Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: Extracting pairs of points from a matrix

  1. Nov 29, 2011 #1
    Probably a basic Mathematica question but after trying a few things I'm stumped.

    I have a table with some x values and a range of data that are dependent on those values, call them a(x), b(x), c(x), etc. So the table looks like

    { {x1, a1, b1, c1}, {x2, a2, b2, c2}, {x3, a3, b3, c3}, etc. }

    I want to create pairs of points like

    { {x1, a1}, {x2, a2}, {x3, a3}, etc. }
    { {x1, b1}, {x2, b2}, {x3, b3}, etc. }

    and so on. That is, I need to create new matrices extracting particular columns from the original matrix. I need these pairs so I can do things like plot a(x) against x (using ListPlot), create InterpolatingFunctions for a(x), b(x), etc. I've tried a few things (Append, Delete, Take) which almost work but don't quite. What's the best Mathematica way to do this?
  2. jcsd
  3. Nov 29, 2011 #2


    User Avatar
    Science Advisor
    Homework Helper

    Something like this?
    Code (Text):

    GetDatasetForColumn[dataset_, i_] := ({#[[1]], #[[i - 1]]}& /@ dataset)
    Last edited: Nov 29, 2011
  4. Nov 29, 2011 #3
    Honestly, I have no idea what that's doing. So we're defining a function GetDataSetForColumn... do I define it as is and then apply it to my matrix? Let's say I've called my matrix "data", and then I run


    I get out

    {{1, Slot}[{11, 12, 13, 14, 15}], {1, Slot}[{21, 22, 23, 24, 25}], {1,
    Slot}[{31, 32, 33, 34, 35}], {1, Slot}[{41, 42, 43, 44, 45}]}

    where for data I'm using {{11, 12, 13, 14, 15}, {21, 22, 23, 24, 25}, {31, 32, 33, 34,
    35}, {41, 42, 43, 44, 45}} as a test case.
  5. Nov 29, 2011 #4


    User Avatar
    Gold Member

    I'm not nearly as good at data manipulation as CompuChip so I almost always end up doing tables and loops, such as :
    Code (Text):

    data = {{x1, a1, b1, c1}, {x2, a2, b2, c2}, {x3, a3, b3, c3}};

    dataout =
     Table[{data[[i]][[1]], data[[i]][[j]]}, {i, 1, Length[data]}, {j, 2, Length[data[[1]]]}]
  6. Nov 29, 2011 #5


    User Avatar
    Science Advisor
    Homework Helper

    Oops, sorry, I forgot a & sign.. I have edited my original post.

    If you have a list {a, b, c, d, e, ...} and a function f, then f /@ {a, b,c, d, e, ...} executes f on each of the elements, so you get { f[a], f, ... } back.
    In this case, the elements of the list are themselves lists, and I define an "anonymous" function
    f[x_] := { x[[1]], x[[i - 1]] }
    that combines the first and (i - 1)th elements (because data column 1 is at index 2, etc).
  7. Nov 29, 2011 #6
    Thanks! I've got it working, and I don't see a need to define a separate function so I've decided to use this command:

    ({#[[1]], #[[3]]} & /@ data)

    To get the first and third columns, etc. I still have no idea why it works so I'll dig through the documentation. Thanks!
  8. Nov 29, 2011 #7
    Still trying to figure out why that works, particularly why I need the &/@ and can't just replace the # with "data". I've found that this works equivalently (again for getting, e.g., first and third columns):

    Transpose[{data[[All, 1]], data[[All, 3]]}]
  9. Nov 29, 2011 #8
    You can simplify (and optimize) that last version a little more, to get

    Code (Text):
    data = {{x1, a1, b1, c1}, {x2, a2, b2, c2}, {x3, a3, b3, c3}};
    data[[All, {1, 3}]]
    And if you want to make a list of all of the pairs, try

    Code (Text):
    Table[data[[All, {1, i}]], {i, 2, Dimensions[data][[2]]}]
    which returns

    Code (Text):
    {{{x1, a1}, {x2, a2}, {x3, a3}},
     {{x1, b1}, {x2, b2}, {x3, b3}},
     {{x1, c1}, {x2, c2}, {x3, c3}}}
  10. Nov 30, 2011 #9
    Simon, this is fantastic, thanks so much!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Similar Threads for Mathematica Extracting pairs
Mathematica Storing Mathematica output
Mathematica Mathematica to MATLAB
Mathematica Cannot do the integral of the Hyper-geometric function?
Mathematica Cannot Plot This Function in Mathematica
Mathematica While Loop in Mathematica