title: Find closest point in matlab grid
body: <p>G'day</p>
  
  <p>I'm trying to program a smart way to find the closest grid points to the points along a contour.</p>
  
  <p>The grid is a 2-dimensional grid, stored in <code>x</code> and <code>y</code> (which contain the x and y kilometre positions of the grid cells).</p>
  
  <p>The contour is a line, made up of x and y locations, not necessarily regularly spaced.</p>
  
  <p>This is shown below - the red dots are the grid, and the blue dots are the points on the contour. How do I find the indices of the red dot closest to each blue dot?</p>
  
  <p><a href="https://i.stack.imgur.com/OzrOJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OzrOJ.png" alt="interpolate red dots closest to each blue dot"></a></p>
  
  <p>Edit - I should mention that the grid is a latitude/longitude grid, of an area fairly close to the south pole. So, the points (the red dots) are the position in metres from the south pole (using a polar stereographic representation). Since the grid is a geographic grid there is unequal grid spacing - with slightly different shaped cells (where the red dots define the vertices of the cells) due to the distortion at high latitudes.
  The result is that I can't just find which row/column of the <code>x</code> and <code>y</code> matrix corresponds closest to the input point coordinates - unlike a regular grid from <code>meshgrid</code>, the values in the rows <em>and</em> columns vary... </p>
  
  <p>Cheers
  Dave</p>