4
$\begingroup$

I have a long list of the form

{{0.005, 1.05, 1.*10^-20}, {0.015, 1.05, 1.*10^-20}, {0.025, 1.05, 
  1.*10^-20}, {0.035, 1.05, 1.*10^-20}, {0.045, 1.05, 
  1.*10^-20}, {0.055, 1.05, 1.*10^-20}, {0.065, 1.05, 
  1.*10^-20}, ........, {3.505, -0.95, 1.*10^-20}}

The first two elements in each list item are coordinates, the third is a function value at those coordinates. I want to do an interpolation so that I can get the function value also at points in between the given coordinates. For that I need to reorder the list into the form

{{{0.005, 1.05}, 1.*10^-20}, {{0.015, 1.05}, 
  1.*10^-20}, {{0.025, 1.05}, 1.*10^-20}, {{0.035, 1.05}, 
  1.*10^-20}, {{0.045, 1.05}, 1.*10^-20}, {{0.055, 1.05}, 
  1.*10^-20}, {{0.065, 1.05}, 1.*10^-20},.......

so that the coordinates are always grouped together. How do I do this best in Mathematica?

$\endgroup$
  • 2
    $\begingroup$ {{#1, #2}, #3} & @@@ list. $\endgroup$ – march Oct 28 '15 at 20:45
  • 1
    $\begingroup$ ...or list /. {x_, y_, z_} -> {{x, y}, z} ;-) $\endgroup$ – mgamer Oct 28 '15 at 20:59
  • 2
    $\begingroup$ Or {Most@#, Last@#}&/@list or Through[{Most, Last} [list]] $\endgroup$ – LLlAMnYP Oct 28 '15 at 23:38
  • 3
    $\begingroup$ Anyone feel like writing an answer here summarizing these answers? P.S. I don't really agree with the close-vote. While the question is simple to answer, I don't think there's a direct answer in the documentation (or at least one that's easy to find). And while the question might appear relatively localized, the original format is a standard data format, but the output format is required for Interpolation, so I feel like this question is relevant for a lot of situations. $\endgroup$ – march Oct 28 '15 at 23:50
  • 5
    $\begingroup$ No one has remarked on the fact that you do not need to format the data like this to use Interpolation. You can just give it triplets like {x,y,f[x,y]} and it will return the appropriate interpolating function. $\endgroup$ – Jason B. Oct 29 '15 at 8:04
6
$\begingroup$

This is to summarize the methods given in the comments.

march

{{#1, #2}, #3} & @@@ list

{#[[1 ;; 2]], #[[3]]} & /@ list

mgamer

list /. {x_, y_, z_} -> {{x, y}, z}

eldo

Transpose[{list[[All, 1 ;; 2]], list[[All, 3]]}]

LLlAMnYP

{Most @ #, Last @ #}& /@ list

Through@*{Most, Last} /@ list

Through[{Most, Last}[#]] & /@ list

m_goldberg

Thread[{list[[All, {1, 2}]], list[[All, 3]]}]

TakeDrop[#, 2]& /@ list 

$\qquad$ TakeDrop is better than Thread because it scans the list only once.

JasonB:

(* Don't bother reframing the list*)
Interpolation[list]
(* works just fine when list={{x1,y1,f[x1,y1]},{x2,y2,f[x2,y2]}...} *)
$\endgroup$
  • $\begingroup$ I liked mgamer's simple solution which worked. Of course, JasonB's is even simpler, but there is nothing about this in the Wolfram Docu. $\endgroup$ – U Mophys Oct 29 '15 at 17:34

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.