Mathematica Mathematica: Extracting pairs of points from a matrix

AI Thread Summary
The discussion revolves around manipulating a matrix in Mathematica to extract specific columns for further analysis, such as plotting and creating interpolating functions. The original matrix contains x values and dependent data points, structured as a list of lists. Users initially attempted various functions like Append, Delete, and Take but found them inadequate for their needs. A key solution involves using an anonymous function to pair the first column with the desired data column, effectively utilizing the syntax `({#[[1]], #[[i - 1]]} & /@ data)`. This approach successfully generates the required pairs. Additionally, alternative methods such as Transpose and direct indexing with `data[[All, {1, 3}]]` are suggested for efficiency. The final recommendation includes using a Table function to create a comprehensive list of pairs from all columns, demonstrating effective data manipulation techniques in Mathematica.
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
Views
1K
Replies
7
Views
1K
Replies
1
Views
2K
Replies
4
Views
5K
Replies
6
Views
10K
Replies
1
Views
11K
Replies
3
Views
7K
Back
Top