4
$\begingroup$

This should be a fairly simple question, but I can't figure out how to do it. I have a list of points of the following form:

{ {{x1, y1}, z1}, {{x2, y2}, z2} ... }

This form is useful for creating an interpolation function. However, if I want to plot a list contour plot of the same data, I need an array of the form:

{ {x1, y1, z1}, {x2, y2, z2} ... }

Is there an easy way to convert between the two, without generating the data again from scratch in the new format? I tried a few things with Flatten[] but that doesn't seem to work for me.

$\endgroup$
  • 7
    $\begingroup$ Flatten /@ list or Append @@@ list should work. $\endgroup$ – J. M. will be back soon Jun 21 '16 at 17:15
  • $\begingroup$ It worked. Thanks! How is Flatten/@list different from Flatten[list,1]? $\endgroup$ – Gowri Jun 21 '16 at 17:17
  • $\begingroup$ Flatten[list,1] basically says "get rid of all the curly braces at level 1". So, if you have a list like {{{1, 2}, 3}, {{4, 2}, 1}}, the elements at level 1 are {{1, 2}, 3} and {{4, 2}, 1}}; remove the outermost braces on each of those, and splice them back into the list, you get {{1, 2}, 3, {4, 2}, 1}. $\endgroup$ – J. M. will be back soon Jun 21 '16 at 17:22
  • $\begingroup$ @J.M. Upvote for that Append. :) $\endgroup$ – yode Jun 21 '16 at 17:32
  • 1
    $\begingroup$ Before this gets closed as a "Simple mistake", there's bound to be a duplicate somewhere it should point to instead $\endgroup$ – Jason B. Jun 21 '16 at 18:44
5
$\begingroup$

Let's have an answer.

data = {{{x1, y1}, z1}, {{x2, y2}, z2}, {{x3, y3}, z3}, {{x4, y4}, z4}};

J.M.

Flatten /@ data
Append @@@ data

m_goldberg

ArrayReshape[data, {Length[data], 3}]
Block[{h}, h[{{a_, b_}, c_}] := {a, b, c}; h /@ data]

All of the above return

{{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}, {x4, y4, z4}}

$\endgroup$
4
$\begingroup$
♭ = ## & @@@ {##} & @@@ # &;

♭ @ {{{x1, y1}, z1}, {{x2, y2}, z2}}

{{x1, y1, z1}, {x2, y2, z2}}

$\endgroup$
  • 1
    $\begingroup$ This looks "exotic" )) +1 $\endgroup$ – e.doroskevic Jun 21 '16 at 19:39
2
$\begingroup$

another option is Cases

lst = {{{x1, y1}, z1}, {{x2, y2}, z2}}
Cases[lst, {{x__}, y__} :> {x, y}]

Mathematica graphics

$\endgroup$
2
$\begingroup$

Example

Code

Partition[Flatten @ data , 3]

Output

{{x1, y1, z1}, {x2, y2, z2}}

Note: data is your original list

Reference

Flatten
Partition

$\endgroup$
  • 2
    $\begingroup$ Could be dangerous if any of the elements are lists themselves. $\endgroup$ – Chip Hurst Jun 21 '16 at 19:56
  • $\begingroup$ @ChipHurst I agree, but OP does not indicate such case or a need for treatment of such cases ;s $\endgroup$ – e.doroskevic Jun 21 '16 at 19:58
2
$\begingroup$

There are many answers, but I want to give another one that could be not so elegant, but is very suitable for easy modification. It's quite often that you have a list of "objects", where object can be a weirdly nested list, and you need to extract some subset of components possible in different order.

l = {{{x1, y1}, z1}, {{x2, y2}, z2}};
{#[[1, 1]], #[[1, 2]], #[[2]]} & /@ l

Here it's really obvious what's happening and if you need different components, you can easily modify indices.

$\endgroup$

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