MATLAB: Closest coordinate points between two data sets

coordinatedsearchnintersectnearestpdist2Statistics and Machine Learning Toolbox

I have two data sets of different sizes, one of which is a 15×3 matrix of latitude, longitude, and concentration data and the other of which is a 2550×3 matrix, also composed of latitude, longitude, and concentration data. I want to find the coordinates in the first data set that are closest to those in the second, larger set. Then based on the index locations of those coordinates in the larger matrix, extract out the concentration values from that correspond to those coordinates so that I can plot the two sets of concentrations against one another. The difficult part of this is that the coordinate locations in the smaller matrix do not align perfectly with those in the larger matrix, so I would like to be able to find the closest possible coordinate locations between the two matrices.
For example:
Matrix 1
A = [
-62.232 47.993 6.3
-63.321 49.888 5.4
-63.442 50.201 2.1
-63.845 51.268 9.7]
Matrix 2
B = [
-62.859 48.918 4.9
-63.987 49.971 3.0
-62.234 47.990 4.6
-62.909 48.236 5.7
-63.325 49.885 7.1
-64.897 48.032 6.3
-63.440 50.200 5.9
-62.921 51.679 3.2
-63.849 51.270 4.5
-64.018 50.327 2.6]
The intersect function does not produce any intersection points because of the mismatch in the coordinates, so I have attempted to use the pdist2 function and dsearchn, but to no avail.
I would like to produce a matrix of indices of the matrix B where the longitude and latitude values are closest to those in matrix A.
For example:
Result (from Matrix B)=
-62.234 47.990 4.6
-63.325 49.885 7.1
-63.440 50.200 5.9
-63.849 51.270 4.5
Indices=
3 3 3
5 5 5
7 7 7
9 9 9
With the end goal being B(indices), allowing me to plot B(:,3) against A(:,3).
Please let me know if you need any more information regarding my issue- thank you!

Best Answer

One way to do it would be to use interp2() in which your X and Y values were your lat and long, and your Z value is corresponding matrix linear indices (integers), and you set the interpolation method to 'nearest'. Then interpolate the other coordinates in order to get out the indices into the first matrix.