6
$\begingroup$

I have the following list

{{{0, 0, 0}, {1, 0, 1}, {2, 0, 2}}, {{0, 1, 1}, {1, 1, 2}, {2, 1, 3}}, {{0, 2, 2}, {1, 2, 3}, {2, 2, 4}}}

but I would like to have, simply, a list of triplets, that is

{{0, 0, 0}, {1, 0, 1}, {2, 0, 2}, {0, 1, 1}, {1, 1, 2}, {2, 1, 3}, {0, 2, 2}, {1, 2, 3}, {2, 2, 4}}

In particular I am trying to use Table to generate some points in a 2D grid to ListPlod3D A 2 function. I have a function of two variables, $f(x,y)$, and I would like to compute it in discrete points in a region of the 2D plane $x$ vs $y$. To do so I need a list like $\{\{x_1,y_1,f(x_1,y_1)\},\{x_2,y_2,f(x_2,y_2)\},...\{x_n,y_n,f(x_n,y_n)\},\}$ as an input for ListPlot3D.

$\endgroup$
1

3 Answers 3

7
$\begingroup$

To do so you can use Level. For example, in your case:

list={{{0, 0, 0}, {1, 0, 1}, {2, 0, 2}}, {{0, 1, 1}, {1, 1, 2}, {2, 1, 3}}, {{0, 2, 2}, {1, 2, 3}, {2, 2, 4}}}

Level[list,{-2}]

This should give the result.

$\endgroup$
7
$\begingroup$

There is a built-in function for just this called Catenate. It can be used for this example as

list = {{{0, 0, 0}, {1, 0, 1}, {2, 0, 2}}, {{0, 1, 1}, {1, 1, 2}, {2, 1, 3}}, {{0, 2, 2}, {1, 2, 3}, {2, 2, 4}}};
Catenate@list

{{0, 0, 0}, {1, 0, 1}, {2, 0, 2}, {0, 1, 1}, {1, 1, 2}, {2, 1, 3}, {0, 2, 2}, {1, 2, 3}, {2, 2, 4}}

Though. if you're just looking for the plot, you can use DiscretePlot3D to generate the plot directly with your given domain. This way you don't even have to worry about data generation and manipulation.

$\endgroup$
6
$\begingroup$

Two more ways:

list = {{{0, 0, 0}, {1, 0, 1}, {2, 0, 2}}, {{0, 1, 1}, {1, 1, 2}, {2, 1, 3}}, {{0, 2, 2}, {1, 2, 3}, {2, 2, 4}}};

Flatten[list, 1]
(*    {{0, 0, 0}, {1, 0, 1}, {2, 0, 2}, {0, 1, 1}, {1, 1, 2}, {2, 1, 3}, {0, 2, 2}, {1, 2, 3}, {2, 2, 4}}    *)

Join @@ list
(*    {{0, 0, 0}, {1, 0, 1}, {2, 0, 2}, {0, 1, 1}, {1, 1, 2}, {2, 1, 3}, {0, 2, 2}, {1, 2, 3}, {2, 2, 4}}    *)
$\endgroup$
1
  • 2
    $\begingroup$ And Cases[list, {__?NumericQ}, All] or Cases[list, _?VectorQ, All] $\endgroup$
    – Bob Hanlon
    May 7, 2023 at 18:09

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