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

15 views (last 30 days)
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?

Answers (2)

Christina
Christina on 15 Jun 2015
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 on 15 Jun 2015
See
doc pdist2
  2 Comments
Christina
Christina on 15 Jun 2015
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 on 15 Jun 2015
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...

Sign in to comment.

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!