Finding values based on coordinates

6 views (last 30 days)
I have two different matrices, the first is a set of decimal coordinates with a value for the carbon density of the soil at those coordinates which is set out as [long, lat, value]. The second is a set of coordinates with land displacement values at the coordinates set out as [long, lat, displacement].
The first matrix has a very large number of points and the second one covers a much smaller section of the first one. I need to create a scrip which takes the coordinates from the second matrix and finds the closest coordinates in the first matrix and then appends the value to a 4th column of the second matrix.
I have to use the closest coordinate as the coordinates of the first matrix are not exactly the same as the coordinates of the second matrix. I have no idea where to start with this, any help or advice will be greatly appreciated.

Accepted Answer

Kelly Kearney
Kelly Kearney on 25 Apr 2014
If you have the Statistics Toolbox, take a look at knnsearch. Depending on the geographic extent of your coordinates, you may need to input a custom distance function to calculate geographic distance instead of cartesian, e.g
dis = @(ltlni, ltlnj) distance(ltlni(:,2), ltlni(:,1), ...
ltlnj(:,2), ltlnj(:,1), ...
referenceEllipsoid('earth'));
[idx, d] = knnsearch(soil(:,1:2), landdis(:,1:2), 'distance', dis);
(That example will require the Mapping Toolbox for distance).

More Answers (2)

John D'Errico
John D'Errico on 25 Apr 2014
Why not use interpolation (i.e., scatteredInterpolant) to infer a value, based on the points around it?
If you really want the nearest point, then MY IPDM can find the index of the closest point in your set. Then use that index to get the value you desire.
  1 Comment
Bill Greene
Bill Greene on 26 Apr 2014
Even with scatteredInterpolant, if you want the nearest point, you can simply change the interpolation method from 'linear' (the default) to 'nearest'.

Sign in to comment.


Geoff Hayes
Geoff Hayes on 25 Apr 2014
Hi Daniel,
The quick (and so not necessarily efficient way!) is to start with the latitude and longitude pair from the first row of the second matrix and compute the squared "distance" between it and each latitude and longitude pair from the first matrix. That distance which is the shortest should correspond to the closest coordinate and so you can grab the carbon density and append it to the fourth column of the second matrix. (If you were to code this up, you would have an outer for loop for the second matrix, and an inner for loop for the first matrix. All distances would need to be considered, and you would just need to keep track of the shortest distance - and in particular the index of the shortest distance into the first matrix - at each iteration of the inner loop.)
A problem with the above is when you compute the difference between the longitude pairs. You have to add extra logic to handle the case where the two longitudes straddle 180 degrees (or the equivalent in radians). For example, if the longitudes are 179 and -179, then the squared difference is (179-(-179))^2 = 358^2. But really the squared difference should be 2^2.
Geoff

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!