Closest coordinate points between two data sets
24 views (last 30 days)
Show older comments
Amy Eisenstadt
on 16 Jul 2017
Edited: Image Analyst
on 17 Jul 2017
I have two data sets of different sizes, one of which is a 15x3 matrix of latitude, longitude, and concentration data and the other of which is a 2550x3 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!
1 Comment
Walter Roberson
on 16 Jul 2017
Please leave A and B in the form of assignment statements so that people can copy and paste them to test and develop their own ideas.
Accepted Answer
Walter Roberson
on 16 Jul 2017
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.
3 Comments
Walter Roberson
on 16 Jul 2017
Edited: Walter Roberson
on 16 Jul 2017
F = scatteredInterpolant(B(:,1), B(:,2), (1:size(B,1)).', 'nearest');
A_is_nearest_idx = F( A(:,1:2) )
B_closest_to_A = B(A_is_nearest_idx, :)
More Answers (1)
Image Analyst
on 17 Jul 2017
Edited: Image Analyst
on 17 Jul 2017
To find the distance of every point in A to every point in B, use pdist2() if you have the stats toolbox:
distances = pdist2(A(:, 1:2), B(:, 1:2))
You get
distances =
1.1175 2.6443 0.0036056 0.71929 2.185 2.6653 2.516 3.7498 3.6542 2.9389
1.0744 0.67115 2.1872 1.7026 0.005 2.4349 0.33392 1.8351 1.4794 0.82373
1.4092 0.59154 2.5195 2.036 0.33696 2.6118 0.0022361 1.5671 1.1439 0.58962
2.5485 1.3048 3.6525 3.1732 1.4775 3.4027 1.1422 1.0113 0.0044721 0.95677
If you want the single closest pair of points, you can do this:
minDistance = min(distances(:))
[rowOfA, rowOfB] = find(distances == minDistance)
There are alternative definitions of closeness for sets of points. Perhaps you'd like to learn about the Hausdorff distance: http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/98/normand/main.html
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!