Find closest values in an array of x and y coordinated compared to a given set of coordinates

16 次查看(过去 30 天)
Hello all,
I've got two vectors of coordinates
x = linspace(400000,500000,100) and
y = linspace(100000,200000,100)
*********
I've got the given coordinates of a point
x_coord= 406000 and y_coord = 170000 and I'd like to find the position/index of the BOTH vectors x and y that are CLOSER to the given coordinate point.
********
I need an index that would fulfill BOTH the nearest values of x and y and at the end I'd get only one index, not two different indices for vectors x and y.
I know that if I had just one vector, I could use the commands:
[c_x index_x] = min(abs(x_coord-x));
[c_y index_y] = min(abs(y_coord-y));
But I don't need that now.
**********
Any thoughts on that?

回答(2 个)

Christina
Christina 2015-6-15
Actually I found the solution:
I created a table with the x and y coordinates:
The small code is:
*********
table[x y];
[d,p] = min((table(:,1)-x_coord).^2 + (table(:,2)-y_coord).^2);
**************
and p gives the index in 'table' matrix that meets the condition of getting the minimum distance between the point (x_coord,y_coord) and both x and y vectors

dpb
dpb 2015-6-15
  2 个评论
Christina
Christina 2015-6-15
Hello
I tried the 'pdist2' command, but when I typed
[D,I] = pdist2(abs(x-x_coord),abs(y-y_coord),distance,'Smallest',k) ;
I get the error that there are not enough input arguments.
Do you know what would be the solution to that?
dpb
dpb 2015-6-15
Rows in {X,Y} to pdist2 are observations so
[dmin,imin]=pdist2([x y],[x0 y0],'euclidean','smallest',1);
is the result the same as your other solution. Not sure which is quicker...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by