title: Meshgrid of z values that match x and y meshgrid values
body: <p><strong>Edit: Original question was flawed but I am leaving it here for reasons of transparency.</strong></p>
  
  <p><strong>Original:</strong>
  I have some x, y, z data where x and y are coordinates of a 2D grid and z is a scalar value corresponding to (x, y). </p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; # Dummy example data 
  &gt;&gt;&gt; x = np.arange(0.0, 5.0, 0.5)
  &gt;&gt;&gt; y = np.arange(1.0, 2.0, 0.1)
  &gt;&gt;&gt; z = np.sin(x)**2 + np.cos(y)**2
  &gt;&gt;&gt; print "x = ", x, "\n", "y = ", y, "\n", "z = ", z
  x =  [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5] 
  y =  [ 1.   1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8  1.9] 
  z =  [ 0.29192658  0.43559829  0.83937656  1.06655187  0.85571064  0.36317266
    0.02076747  0.13964978  0.62437081  1.06008127]
  </code></pre>
  
  <p>Using <code>xx, yy = np.meshgrid(x, y)</code> I can get two grids containing x and y values corresponding to each grid position. </p>
  
  <pre><code>&gt;&gt;&gt; xx, yy = np.meshgrid(x, y)
  &gt;&gt;&gt; print xx
  [[ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
   [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]]
  &gt;&gt;&gt; print yy
  [[ 1.   1.   1.   1.   1.   1.   1.   1.   1.   1. ]
   [ 1.1  1.1  1.1  1.1  1.1  1.1  1.1  1.1  1.1  1.1]
   [ 1.2  1.2  1.2  1.2  1.2  1.2  1.2  1.2  1.2  1.2]
   [ 1.3  1.3  1.3  1.3  1.3  1.3  1.3  1.3  1.3  1.3]
   [ 1.4  1.4  1.4  1.4  1.4  1.4  1.4  1.4  1.4  1.4]
   [ 1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5]
   [ 1.6  1.6  1.6  1.6  1.6  1.6  1.6  1.6  1.6  1.6]
   [ 1.7  1.7  1.7  1.7  1.7  1.7  1.7  1.7  1.7  1.7]
   [ 1.8  1.8  1.8  1.8  1.8  1.8  1.8  1.8  1.8  1.8]
   [ 1.9  1.9  1.9  1.9  1.9  1.9  1.9  1.9  1.9  1.9]]
  </code></pre>
  
  <p>Now I want an array of the same shape for z, where the grid values correspond to the matching x and y values in the original data! But I cannot find an elegant, built-in solution where I do not need to re-grid the data, and I think I am missing some understanding of how I should approach it. </p>
  
  <p>I have tried following <a href="https://stackoverflow.com/questions/15118939/fill-2d-numpy-array-from-three-1d-numpy-arrays/15120881#15120881">this</a> solution (with my real data, not this simple example data, but it should have the same result) but my final grid was not fully populated. 
  Please help! </p>
  
  <p><strong>Corrected question:</strong></p>
  
  <p><strong>As was pointed out by commenters, my original dummy data was unsuitable for the question I am asking. Here is an improved version of the question:</strong> </p>
  
  <p>I have some x, y, z data where x and y are coordinates of a 2D grid and z is a scalar value corresponding to (x, y). The data is read from a text file "data.txt":</p>
  
  <pre><code>#x y z
  1.4 0.2 1.93164166734
  1.4 0.3 1.88377897779
  1.4 0.4 1.81946452501
  1.6 0.2 1.9596778849
  1.6 0.3 1.91181519535
  1.6 0.4 1.84750074257
  1.8 0.2 1.90890970517
  1.8 0.3 1.86104701562
  1.8 0.4 1.79673256284
  2.0 0.2 1.78735230743
  2.0 0.3 1.73948961789
  2.0 0.4 1.67517516511
  </code></pre>
  
  <p>Loading the text:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; inFile = 'C:\data.txt' 
  &gt;&gt;&gt; x, y, z = np.loadtxt(inFile, unpack=True, usecols=(0, 1, 2), comments='#', dtype=float)
  &gt;&gt;&gt; print x
  [ 1.4  1.4  1.4  1.6  1.6  1.6  1.8  1.8  1.8  2.   2.   2. ]
  &gt;&gt;&gt; print y
  [ 0.2  0.3  0.4  0.2  0.3  0.4  0.2  0.3  0.4  0.2  0.3  0.4]
  &gt;&gt;&gt; print z
  [ 1.93164167  1.88377898  1.81946453  1.95967788  1.9118152   1.84750074
    1.90890971  1.86104702  1.79673256  1.78735231  1.73948962  1.67517517]
  </code></pre>
  
  <p>Using <code>xx, yy= np.meshgrid(np.unique(x), np.unique(y))</code> I can get two grids containing x and y values corresponding to each grid position. </p>
  
  <pre><code>&gt;&gt;&gt; xx, yy= np.meshgrid(np.unique(x), np.unique(y))
  &gt;&gt;&gt; print xx
   [[ 1.4  1.6  1.8  2. ]
   [ 1.4  1.6  1.8  2. ]
   [ 1.4  1.6  1.8  2. ]]
  &gt;&gt;&gt; print yy
  [[ 0.2  0.2  0.2  0.2]
   [ 0.3  0.3  0.3  0.3]
   [ 0.4  0.4  0.4  0.4]]
  </code></pre>
  
  <p>Now each corresponding cell position in both xx and yy correspond to one of the original grid point locations. 
  I simply need an equivalent array where the grid values correspond to the matching z values in the original data! </p>
  
  <pre><code>"""e.g. 
  [[ 1.93164166734  1.9596778849  1.90890970517  1.78735230743]
   [ 1.88377897779  1.91181519535  1.86104701562  1.73948961789]
   [ 1.81946452501  1.84750074257  1.79673256284  1.67517516511]]"""
  </code></pre>
  
  <p>But I cannot find an elegant, built-in solution where I do not need to re-grid the data, and I think I am missing some understanding of how I should approach it. For example, using <code>xx, yy, zz = np.meshgrid(x, y, z)</code> returns three 3D arrays that I don't think I can use. </p>
  
  <p>Please help! </p>
  
  <p><strong>Edit:</strong>
  I managed to make this example work thanks to the solution from Jaime: <a href="https://stackoverflow.com/questions/15118939/fill-2d-numpy-array-from-three-1d-numpy-arrays/15120881#15120881">Fill 2D numpy array from three 1D numpy arrays</a></p>
  
  <pre><code>&gt;&gt;&gt; x_vals, x_idx = np.unique(x, return_inverse=True)
  &gt;&gt;&gt; y_vals, y_idx = np.unique(y, return_inverse=True)
  &gt;&gt;&gt; vals_array = np.empty(x_vals.shape + y_vals.shape)
  &gt;&gt;&gt; vals_array.fill(np.nan) # or whatever your desired missing data flag is
  &gt;&gt;&gt; vals_array[x_idx, y_idx] = z
  &gt;&gt;&gt; zz = vals_array.T
  &gt;&gt;&gt; print zz
  </code></pre>
  
  <p>But the code (with <em>real</em> input data) that led me on this path was still failing. I found the problem now. I have been using <code>scipy.ndimage.zoom</code> to resample my gridded data to a higher resolution <strong>before</strong> generating zz. </p>
  
  <pre><code>&gt;&gt;&gt; import scipy.ndimage
  &gt;&gt;&gt; zoom = 2
  &gt;&gt;&gt; x =  scipy.ndimage.zoom(x, zoom)
  &gt;&gt;&gt; y =  scipy.ndimage.zoom(y, zoom)
  &gt;&gt;&gt; z =  scipy.ndimage.zoom(z, zoom)
  </code></pre>
  
  <p>This produced an array containing many nan entries:</p>
  
  <pre><code>array([[ nan,  nan,  nan, ...,  nan,  nan,  nan],
         [ nan,  nan,  nan, ...,  nan,  nan,  nan],
         [ nan,  nan,  nan, ...,  nan,  nan,  nan],
         ..., 
         [ nan,  nan,  nan, ...,  nan,  nan,  nan],
         [ nan,  nan,  nan, ...,  nan,  nan,  nan],
         [ nan,  nan,  nan, ...,  nan,  nan,  nan]])
  </code></pre>
  
  <p>When I skip the zoom stage, the correct array is produced:</p>
  
  <pre><code>array([[-22365.93400183, -22092.31794674, -22074.21420168, ...,
          -14513.89091599, -12311.97437017, -12088.07062786],
         [-29264.34039242, -28775.79743097, -29021.31886353, ...,
          -21354.6799064 , -21150.76555669, -21046.41225097],
         [-39792.93758344, -39253.50249278, -38859.2562673 , ...,
          -24253.36838785, -25714.71895023, -29237.74277727],
         ..., 
         [ 44829.24733543,  44779.37084337,  44770.32987311, ...,
           21041.42652441,  20777.00408692,  20512.58162671],
         [ 44067.26616067,  44054.5398901 ,  44007.62587598, ...,
           21415.90416488,  21151.48168444,  20887.05918082],
         [ 43265.35371973,  43332.5983711 ,  43332.21743471, ...,
           21780.32283309,  21529.39770759,  21278.47255848]])
  </code></pre>