Finding values based on coordinates
이전 댓글 표시
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.
채택된 답변
추가 답변 (2개)
John D'Errico
2014년 4월 25일
0 개 추천
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
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
2014년 4월 25일
0 개 추천
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
카테고리
도움말 센터 및 File Exchange에서 Geometric Geodesy에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!