Mathematica: Extracting pairs of points from a matrix

In summary, the best way to create pairs of points from a table in Mathematica is to use the Transpose function to extract particular columns from the original matrix and then use the Table function to create a list of all the pairs. Alternatively, you can use a combination of the Part function and the All property to extract and combine specific columns from the original matrix.
  • #1
ramparts
45
0
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?
 
Physics news on Phys.org
  • #2
Something like this?
Code:
GetDatasetForColumn[dataset_, i_] := ({#[[1]], #[[i - 1]]}& /@ dataset)
 
Last edited:
  • #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

GetDatasetForColumn[data,1]

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.
 
  • #4
I'm not nearly as good at data manipulation as CompuChip so I almost always end up doing tables and loops, such as :
Code:
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]]]}]
 
  • #5
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).
 
  • #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!
 
  • #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]]}]
 
  • #8
You can simplify (and optimize) that last version a little more, to get

Code:
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:
Table[data[[All, {1, i}]], {i, 2, Dimensions[data][[2]]}]

which returns

Code:
{{{x1, a1}, {x2, a2}, {x3, a3}}, 
 {{x1, b1}, {x2, b2}, {x3, b3}}, 
 {{x1, c1}, {x2, c2}, {x3, c3}}}
 
  • #9
Simon, this is fantastic, thanks so much!
 

Related to Mathematica: Extracting pairs of points from a matrix

1. How can I extract pairs of points from a matrix in Mathematica?

To extract pairs of points from a matrix in Mathematica, you can use the function Cases with a pattern that specifies the format of the pairs you want to extract.

2. What is the syntax for using the Cases function to extract pairs of points from a matrix?

The syntax for using Cases to extract pairs of points from a matrix is Cases[list, pattern], where list is the matrix and pattern specifies the format of the pairs you want to extract.

3. Can I extract pairs of points with specific criteria from a matrix using Mathematica?

Yes, you can use Cases with a more specific pattern to extract pairs of points that meet certain criteria from a matrix. For example, you can specify a condition for the x or y values of the points.

4. Is there a way to extract pairs of points from a matrix in a specific order?

Yes, you can use the Sort function in conjunction with Cases to extract pairs of points from a matrix in a specific order. You can specify the sorting criteria, such as ascending or descending order, for the x or y values of the points.

5. Can I extract pairs of points from a matrix and store them in a new list or matrix?

Yes, you can use Cases to extract pairs of points from a matrix and then use the Table or Array functions to store them in a new list or matrix. You can also use the Part function to extract specific pairs from the matrix and store them in a new list or matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
928
  • Engineering and Comp Sci Homework Help
Replies
7
Views
930
Replies
7
Views
723
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Quantum Interpretations and Foundations
2
Replies
38
Views
4K
Replies
8
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
10K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top