I have three arrays of the same length in Matlab, `X`

, `Y`

and `Z`

. `X(i)`

, `Y(i)`

and `Z(i)`

forms a pair of 3D coordinates.

The issue now is, how to use these three arrays to generate square meshes, as shown below:

I got the image from mesh plot in Matlab documentation. So obviously `mesh`

command is not what I want because it plots the meshes itself in the Matlab program, whereas I need the mesh elements ( along with the coordinates) so that I can plot them out myself in other program, such as C#.

In other words, I am looking for the *mathematical algorithm* to generate the meshes that allows `mesh`

command to plot the below looking graph.

**Edit: I realized that my question wasn't clear after a good night sleep. So here's more detail. I generate x and y vector by using this command [x,y]=meshgrid[rangex, rangy], and then I define a vector z with the function z(x,y). I would have to return a list of square elements ( as shown in the figure below) along with their corresponding x,y coordinates. So basically I just want to replot the following graph with those data.**

Any ideas?

## Best Solution

What you basically have are 3 matrices:

Now you have to reshape these three matrices into row vectors:

and another one of indices:

and now your list is:

For instance:

Now

`result`

holds the indeces in the first column, and`(x,y,z)`

in the rest of the columns. You should be able to extract what you want from there.