2
$\begingroup$

I have a matrix data in which column 5 holds x-coordinates, and then columns 6 through 37 each holds sets of y-coordinates. I want to map x, y pairs, keeping the pairs separated according to each set of y-coordinates. This would form a nested structure like this:

{...
{{1., 1812.}, {2., 10076.}, {3., 4764}}, 
{{1., 6839.}, {2., 3849.}, {3., 2746}},  
{{1., 6839.}, {2., 3849.}, {3., 2746}}
...}

where the first sublist holds x, y pairs for column 5, the second sublist holds x, y pairs for column 6, and so on until column 37.

I've managed to get x, y pairs for the first set of y-coordinates:

Transpose[{data[[All, 5]], data[[All, 6]]}]

This gives me

{{1., 1812.}, {2., 10076.}, {3., 4764.}}

How can I also get x, y pairs for the remaining sets of y-coordinates?

$\endgroup$
  • $\begingroup$ Is the x-coordinates column duplicate-free? $\endgroup$ – kglr Jul 7 '18 at 21:58
  • $\begingroup$ Not in my dataset, no. I happen to have repeating x-coordinates of 1, 2, 3, all with corresponding y-values. I truncated it here. @kglr $\endgroup$ – briennakh Jul 7 '18 at 22:04
2
$\begingroup$

If your data structure is as presented, then I believe the following should work:

Inner[List, data[[All, 5]], data[[All, 6 ;; 37]], List]

Inner is a very powerful function for manipulating rectangular data structures, would recommend looking into its use. To understand what's actually going on here, I'd recommend starting with:

Inner[f, data[[All, 5]], data[[All, 6 ;; 37]], g]

Assuming that f and g are undefined.

$\endgroup$
  • $\begingroup$ @briennakh Made a change after taking a closer look at the data structure. It looks like it should work now. $\endgroup$ – eyorble Jul 7 '18 at 21:01
  • $\begingroup$ It works! Thanks! It's good to know about Inner $\endgroup$ – briennakh Jul 7 '18 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.