# Reduce matrix to a vector [closed]

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.

• May 7, 2023 at 18:11

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.

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.

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}}    *)
• And Cases[list, {__?NumericQ}, All] or Cases[list, _?VectorQ, All] May 7, 2023 at 18:09