Finding values based on coordinates

조회 수: 4 (최근 30일)
Daniel
Daniel 2014년 4월 25일
댓글: Bill Greene 2014년 4월 26일
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.

채택된 답변

Kelly Kearney
Kelly Kearney 2014년 4월 25일
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).
  댓글 수: 1
Daniel
Daniel 2014년 4월 26일
Thank-you that worked perfectly

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

John D'Errico
John D'Errico 2014년 4월 25일
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
Bill Greene
Bill Greene 2014년 4월 26일
Even with scatteredInterpolant, if you want the nearest point, you can simply change the interpolation method from 'linear' (the default) to 'nearest'.

댓글을 달려면 로그인하십시오.


Geoff Hayes
Geoff Hayes 2014년 4월 25일
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

카테고리

Help CenterFile Exchange에서 Geometric Geodesy에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by