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?
Answer
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]}...} *)
Comments
Post a Comment