finding closer points to a given co-ordinates
43 views (last 30 days)
Hi, everyone: I have two centers P(x1,y1) and Q(x2,y2). In each coordinate there are cluster of 10 points normally distributed with standard deviation 2,and mean 0. How to write code in matlab to find closer points around the P and Q and compare? If anybody have idea please share. Thank you.
Image Analyst on 18 May 2018
Edited: Image Analyst on 18 May 2018
% Find point in x1 and y1 vectors that is closest to point P with coordinates (Px, Py).
distancesP = sqrt((x1 - Px).^2 + (y1 - Py) .^ 2);
[minDistanceP, indexOfMinP] = min(distancesP);
% Find point in x2 and y2 vectors that is closest to point Q with coordinates (Qx, Qy).
distancesQ = sqrt((x2 - Qx).^2 + (y2 - Qy) .^ 2);
[minDistanceQ, indexOfMinQ] = min(distancesQ);
Ameer Hamza on 18 May 2018
Edited: Ameer Hamza on 18 May 2018
If cluster 1 elemets are arranged like this
X1 = [x1 y1;
and P is a row vector than to find the minimum distance point use
[minValue, minIndex] = min(vecnorm(X1 - P, 2, 2));
similarly, you can find points closest to Q.
John BG on 19 May 2018
Edited: John BG on 19 May 2018
Hi Subarna Giri
this is John BG <mailto:email@example.com firstname.lastname@example.org>
On April 30th I answered the really similar question
nearest point from two matrices
My answer to that question not only finds the distance between the nearest points of 2 sets, like P and Q in your question, but also returns
a matrix Da that has all distances among all points ordered by proximity.
a matrix Na ranking the elements of both sets by proximity, it may also come handy, may it not?
Subarna, replace the following A and B with the sets P and Q of your question.
If you supply P and Q I will update my answer with the coordinates you make available.
clear all;clc;close all
axis([0 50 0 50])
text(A(k,1),A(k,2),[' ' num2str(Ln(k))],'FontSize',12,'Color','red')
plot(B(:,1),B(:,2),'*','Color',[.2 .7 .2]);grid on
text(B(k,1),B(k,2),[' ' num2str(Ln(k))],'FontSize',12,'Color',[.2 .7 .2])
Now let's define 2 matrices for the results
Da2 is for the distances of each element of A to all elements of B.
Na2 is for the ordered numerals according to distance, the 1st is the nearest.
% L1=[1:k];L1(end)=;L1=[L1 k+1:N] % numerals of all B neighbour except kth neighbour
Da=(sum((pa-pb).^2,2)).^.5 % distance of a point to all B neighbours
Da2(k,:)=D0(:,1) % update sorted distances
Na2(k,:)=D0(:,2) % update sorted numerals
3 4 5 1 6 2
6 2 5 4 1 3
4 1 2 3 6 5
2 6 1 4 5 3
4 3 1 5 2 6
1 4 2 6 3 5
2 1 6 4 5 3
5 6 2 3 4 1
2 6 1 4 5 3
6 2 4 1 5 3
If you focus on the 1st column
reading it as follows
1st element of A - GREEN, is closest to 3rd element of B - RED.
2nd element of A - GREEN, is closest to 6th element of B - RED.
3rd element of A - GREEN, is closest to 4th element of B - RED.
4th element of A - GREEN, is closest to 2nd element of B - RED.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention