title: Maintaining point correlation while flattening 2D meshgrid
body: <p>How can I flatten three separate two dimensional arrays, while maintaining point correlation? For example, I am creating a meshgrid from a dataset containing position information for x and y, and some correlated data array (cartm). After the interpolation, <code>X2</code> and <code>Y2</code> have size 300x300 due to the meshgrid; <code>interpval</code> also size 300x300, since each <code>interpval</code> point is correlated with a single coordinate on the (X2,Y2) meshgrid.</p>
  
  <p>How can I flatten each of these 2d matrices, and maintain the 1:1 correlation between values of <code>interpval</code> and their location on the meshgrid? Ultimately, I would like to end up with an Nx3 array, with an X-location column, a Y-location column, and a column with the corresponding interpolation data. Thanks in advance!</p>
  
  <pre><code>import numpy as np
  import scipy.interpolate
  import matplotlib.pyplot as plt
  
  # Create x,y vectors
  cartx = np.array([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50])
  carty = np.array([1, 23, 4, 6, 12, 15, 16, 18, 20, 21, 22])
  
  # Create data vector
  cartm = np.array([5.3, 23, 2, 5, 2, 2.5, 13, 9, 7.5, 1.7, 12])
  
  # Prepare meshgrid, interpolate
  cartcoord = zip(cartx, carty)
  X = np.linspace(cartx.min(), cartx.max(), 300)
  Y = np.linspace(carty.min(), carty.max(), 300)
  X2, Y2 = np.meshgrid(X, Y)
  interp = scipy.interpolate.LinearNDInterpolator(cartcoord, cartm, fill_value=0)
  interpval = interp(X2, Y2)
  
  print(X2.shape, Y2.shape, interpval.shape)
  </code></pre>