Mathematica: Extracting pairs of points from a matrix

Click For Summary

Discussion Overview

The discussion revolves around extracting pairs of points from a matrix in Mathematica, specifically focusing on how to manipulate a dataset structured with x values and corresponding dependent data. Participants explore various methods to create new matrices that extract specific columns for further analysis, such as plotting and interpolation.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant describes a matrix structure with x values and dependent data and seeks a method to extract pairs of points for plotting and interpolation.
  • Another participant suggests a function, GetDatasetForColumn, to extract a specific column from the dataset, but there is confusion about its implementation.
  • A different participant shares a method using loops and tables to achieve similar results, indicating a preference for more straightforward data manipulation techniques.
  • There is a discussion about using anonymous functions and the syntax required to apply them effectively to the dataset.
  • One participant finds a working solution using a concise command to extract specific columns without defining a separate function, expressing a desire to understand the underlying mechanics.
  • Another participant proposes using Transpose to achieve the same goal, highlighting an alternative approach to data extraction.
  • A suggestion is made to simplify the extraction process further by directly indexing the data matrix, which is acknowledged as an optimization.
  • Finally, a participant expresses appreciation for the shared solutions and insights provided by others in the thread.

Areas of Agreement / Disagreement

Participants generally agree on the methods to extract data from the matrix, with multiple approaches being discussed. There is no clear consensus on a single best method, as different participants prefer different techniques based on their familiarity and comfort with Mathematica.

Contextual Notes

Some participants express uncertainty about the syntax and functionality of certain commands in Mathematica, indicating a need for further exploration of the documentation. The discussion also highlights varying levels of expertise among participants, which influences their preferred methods of data manipulation.

Who May Find This Useful

This discussion may be useful for Mathematica users looking to manipulate datasets, particularly those interested in extracting specific columns for analysis or visualization in scientific and engineering contexts.

ramparts
Messages
44
Reaction score
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
Something like this?
Code:
GetDatasetForColumn[dataset_, i_] := ({#[[1]], #[[i - 1]]}& /@ dataset)
 
Last edited:
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.
 
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]]]}]
 
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).
 
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!
 
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]]}]
 
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}}}
 
Simon, this is fantastic, thanks so much!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 6 ·
Replies
6
Views
10K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
7K