title: Generate Square Mesh, Given an Unordered X, Y and Z Vectors
body: <p>I have three arrays of the same length in Matlab, <code>X</code>, <code>Y</code> and <code>Z</code>. <code>X(i)</code>, <code>Y(i)</code> and <code>Z(i)</code> forms a pair of 3D coordinates.</p>
  
  <p>The issue now is, how to use these three arrays to generate square meshes, as shown below:
  <a href="https://i.stack.imgur.com/r81e7.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r81e7.gif" alt="alt text"></a></p>
  
  <p>I got the image from <a href="https://www.mathworks.com/help/matlab/ref/meshz.html;jsessionid=164c9fe7ffbd934e994fb2cafcf5" rel="nofollow noreferrer">mesh plot</a> in Matlab documentation. So obviously <code>mesh</code> 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#.</p>
  
  <p>In other words, I am looking for the <em>mathematical algorithm</em> to generate the meshes that allows <code>mesh</code> command to plot the below looking graph. </p>
  
  <p><strong>Edit: I realized that my question wasn't clear after a good night sleep. So here's more detail. I generate <code>x</code> and <code>y</code> vector by using this command <code>[x,y]=meshgrid[rangex, rangy]</code>, and then I define a vector z with the function <code>z(x,y)</code>. I would have to return a list of square elements ( as shown in the figure below) along with their corresponding <code>x</code>,<code>y</code> coordinates. So basically I just want to replot the following graph with those data.</strong></p>
  
  <p>Any ideas?</p>