finding closer points to a given co-ordinates

조회 수: 52 (최근 30일)
Subarna Giri
Subarna Giri 2018년 5월 18일
댓글: Subarna Giri 2018년 5월 19일
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

답변 (3개)

Image Analyst
Image Analyst 2018년 5월 18일
편집: Image Analyst 2018년 5월 18일
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
Image Analyst
Image Analyst 2018년 5월 19일
편집: Image Analyst 2018년 5월 19일
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
Subarna Giri 2018년 5월 19일
I will show you, when completed.

댓글을 달려면 로그인하십시오.


Ameer Hamza
Ameer Hamza 2018년 5월 18일
편집: Ameer Hamza 2018년 5월 18일
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.

John BG
John BG 2018년 5월 19일
편집: John BG 2018년 5월 19일
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

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by