How to find nearest two points each other ?

Hi Everyone,
I have
X = (Yt.*sin(W))-(Xt.*cos(W));
Y = (Yt.*cos(W))+(Xt.*sin(W)); which give the coordinates X and Y.
X_inv = R.*sin(B_involute);
Y_inv = R.*cos(B_involute); which give the coordinates X_inv and Y_inv.
I need to find the nearest two points between X,Y and X_inv,Y_inv.
lots of thanks from now.

2 commentaires

Image Analyst
Image Analyst le 29 Nov 2015
What is W? What is B_involute? Can you post a diagram or plot of this and point out X, Y, X_inv, Y_inv what two points on there you'd like to have it return - which are the "two points"? And it's ambiguous. What's nearest to what? Two points nearest to each other? Two points nearest to either X,Y or X_inv,Yinv? For example if you have 2,6,7,50, then 6 and 7 are nearest to each other but 6 and 7 are closer to 2 than they are to 50, so would you return 6 and 7, or return 2???
Ender Rencuzogullari
Ender Rencuzogullari le 29 Nov 2015
Modifié(e) : Ender Rencuzogullari le 29 Nov 2015
sorry for confusion. I have program to plot graph of two curves by specifying the coodinates of points. And, those curves intersect at an unknown point. I want to find out this point but If can find the nearest points between both, that will be my solution. X,Y specify a point while loop, and X_inv, Y_inv specify other point's coordinate. In fact, A=[X Y] and B=[X_inv Y_inv]. to make more clarify, I attached plot image of those graphs. the blue marks on 1st curve show the coordinates of point depends on X_inv (x axis) and Y_inv (y axis). it is the same with second curve depends on X and Y.Now, I want to find out the nearest point on second curve to point on first curve. I hope I could use english to explain enough.

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 29 Nov 2015

2 votes

If you have the Statistics Toolbox, use the pdist2 function.

4 commentaires

I don't. Even If I had it, I do not know how to transform my program from Matlab to this toolbox. Could you help me with Matlab?
It’s a MATLAB toolbox, so no transformations are necessary. Just use its functions as you would any other in core MATLAB. It assumes ‘X’, ‘Y’, ‘X_inv’ and ‘Y_inv’ are vectors.
If you don’t have pdist2, this will work:
X = randi(99, 10, 1);
Y = randi(99, 10, 1);
X_inv = randi(99, 10, 1);
Y_inv = randi(99, 10, 1);
for k1 = 1:length(X)
for k2 = 1:length(X_inv)
DE(k1,k2) = hypot(X(k1)-X_inv(k2), Y(k1)-Y_inv(k2)); % Euclidean Distance
end
end
[DEmin,ix] = min(DE(:));
[K1,K2] = ind2sub(size(DE),ix);
fprintf(1,'\nNearest Points:\n\tDistance = %.3f\n', DEmin)
fprintf(1,'\t\tX(%d), Y(%d) \t\t\t= %.2f, %.2f\n', K1, K1, X(K1), Y(K1))
fprintf(1,'\t\tX_inv(%d), Y_inv(%d) \t= %.2f, %.2f\n', K2, K2, X_inv(K2), Y_inv(K2))
I created my own vectors to test and demonstrate my code. Substitute yours. You do not need to make any other changes. My vectors are column vectors, and I did not test my code with row vectors, but I doubt that will be a problem.
This will only detect the first minimum value it encounters, even if there are more with the same value. You would need to use the find function if there are more than one.
Ender Rencuzogullari
Ender Rencuzogullari le 29 Nov 2015
Modifié(e) : Ender Rencuzogullari le 29 Nov 2015
Thank You Sir, It works excellent !
Star Strider
Star Strider le 29 Nov 2015
Modifié(e) : Star Strider le 29 Nov 2015
My pleasure!
You would simply use your ‘X’, ‘Y’, ‘X_inv’ and ‘Y_inv’ in my code, eliminating the lines I used to create them to test it.
You would use only:
for k1 = 1:length(X)
for k2 = 1:length(X_inv)
DE(k1,k2) = hypot(X(k1)-X_inv(k2), Y(k1)-Y_inv(k2)); % Euclidean Distance
end
end
[DEmin,ix] = min(DE(:));
[K1,K2] = ind2sub(size(DE),ix);
fprintf(1,'\nNearest Points:\n\tDistance = %.3f\n', DEmin)
fprintf(1,'\t\tX(%d), Y(%d) \t\t\t= %.2f, %.2f\n', K1, K1, X(K1), Y(K1))
fprintf(1,'\t\tX_inv(%d), Y_inv(%d) \t= %.2f, %.2f\n', K2, K2, X_inv(K2), Y_inv(K2))

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 29 Nov 2015

1 vote

Use a nested for loop and the sqrt() function, then sort() and find() to find the 8 closest distances at the two points where your curves intersect.
Or maybe you could use roots(curve1-curve2).
Semion
Semion le 14 Avr 2020

0 votes

Hi. Could you explain, how does method "dsearchn" select an index of multi closest points with the same distance to target point? BW, the method "dnsearch" with and without triangulation produce different results.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by