# finding closer points to a given co-ordinates

30 views (last 30 days)
Subarna Giri on 18 May 2018
Commented: Subarna Giri on 19 May 2018
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.
Regards; SGiri
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Answers (3)

Image Analyst on 18 May 2018
Edited: Image Analyst on 18 May 2018
Try this:
% 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);
##### 8 CommentsShow 6 older commentsHide 6 older comments
Image Analyst on 19 May 2018
Edited: Image Analyst on 19 May 2018
Well then perhaps it's because of the sentence "Use these numbers as indices to select 20 points from the total of 50 points." Use WHAT numbers? How exactly would the mean, standard deviation of distance, and the location of the reference points be used as indices? In what way could those numbers be used to make the selection? I couldn't see how so I just chose them randomly.
Subarna Giri on 19 May 2018
I will show you, when completed.

Sign in to comment.

Ameer Hamza on 18 May 2018
Edited: Ameer Hamza on 18 May 2018
If cluster 1 elemets are arranged like this
X1 = [x1 y1;
x2 y2;
....
....
x10 y10];
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.
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

John BG on 19 May 2018
Edited: John BG on 19 May 2018
Hi Subarna Giri
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
1.-
a matrix Da that has all distances among all points ordered by proximity.
2.-
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
N=10
Ln=[1:N]
A=randi([0 50],N,2);
B=randi([0 50],N,2);
plot(A(:,1),A(:,2),'r*');grid on
axis([0 50 0 50])
axis equal
hold on
for k=1:1:N
text(A(k,1),A(k,2),[' ' num2str(Ln(k))],'FontSize',12,'Color','red')
end
plot(B(:,1),B(:,2),'*','Color',[.2 .7 .2]);grid on
for k=1:1:N
text(B(k,1),B(k,2),[' ' num2str(Ln(k))],'FontSize',12,'Color',[.2 .7 .2])
end
.
.
Now let's define 2 matrices for the results
2.-
Da2=zeros(Na,Nb)
.
Da2 is for the distances of each element of A to all elements of B.
.
Na2=zeros(Na,Nb)
.
Na2 is for the ordered numerals according to distance, the 1st is the nearest.
for k=1:1:Na
% L1=[1:k];L1(end)=[];L1=[L1 k+1:N] % numerals of all B neighbour except kth neighbour
pa=repmat(A(k,:),Nb,1)
Da=(sum((pa-pb).^2,2)).^.5 % distance of a point to all B neighbours
D0=sortrows([Da Lnb'])
Da2(k,:)=D0(:,1) % update sorted distances
Na2(k,:)=D0(:,2) % update sorted numerals
end
3.-
The results
Na2 =
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
Na2(:,1)
=
3
6
4
2
4
1
2
5
2
6
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.
.
Subarna
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
John BG
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Descriptive Statistics in Help Center and File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!