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

조회 수: 15 (최근 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