find the furthest point
Afficher commentaires plus anciens
if i have an two arrays like that:
x=[ 39 63 71 7 53 39 64 23 29 65 ];
y=[ 20 11 22 78 61 71 9 20 78 94 ];
and
AA=[x ; y];
if p1 = (39,20) and p2 =(63,11)
how can i find the furthest point in AA than the points p1 and p2
Thanks, Hrabei
3 commentaires
Azzi Abdelmalek
le 27 Sep 2013
[Hassan commented]
Thanks for the answers, however, the question is"how can i find the furthest point in AA than the points p1 and p2"
i.e.
what is the point (only one point) in AA that is furthest from p1 and p2
Azzi Abdelmalek
le 27 Sep 2013
Modifié(e) : Azzi Abdelmalek
le 27 Sep 2013
Hassan, If you have two point p1 and p2, and two other points p3 and p4, how will you choose p3 rather than p4. What is your mathematics criterion?
Image Analyst
le 27 Sep 2013
The phrase "how can i find the furthest point in AA than the points p1 and p2" does not make grammatical sense. Do you really mean "how can i find the furthest point in AA from the points p1 and p2"??? In other words, find the point in AA farthest from point p1, and then also find the furthest point in AA from point p2? And why are p1 and p2 already inside AA ? Do they need to be, or is that just a coincidence? Please clarify.
Réponses (2)
Image Analyst
le 26 Sep 2013
Modifié(e) : Image Analyst
le 27 Sep 2013
Try this:
x=[ 39 63 71 7 53 39 64 23 29 65 ];
y=[ 20 11 22 78 61 71 9 20 78 94 ];
AA=[x ; y]
% Find point in AA closest to p1.
p1 = [39,20]
squaredDistance = sum((AA-repmat(p1', [1, size(AA, 2)])).^2, 1)
[maxSqDist1, indexOfMax1] = max(squaredDistance)
% Find point in AA closest to p2.
p2 =[63,11]
squaredDistance = sum((AA-repmat(p2', [1, size(AA, 2)])).^2, 1)
[maxSqDist2, indexOfMax2] = max(squaredDistance)
In the command window, you'll see:
AA =
39 63 71 7 53 39 64 23 29 65
20 11 22 78 61 71 9 20 78 94
p1 =
39 20
squaredDistance =
0 657 1028 4388 1877 2601 746 256 3464 6152
maxSqDist1 =
6152
indexOfMax1 =
10
p2 =
63 11
squaredDistance =
657 0 185 7625 2600 4176 5 1681 5645 6893
maxSqDist2 =
7625
indexOfMax2 =
4
4 commentaires
Jan
le 27 Sep 2013
@Image Analyst: "the furthest point" sounds like max() is wanted instead of min().
@Hassan: Notice that this solution uses the squared distance. The result is the same, because the maximal squared distance matches the same point as the maximal distance. But omitting the square root accelerates the program, because sqrt() is a very expensive function.
Image Analyst
le 27 Sep 2013
Modifié(e) : Image Analyst
le 27 Sep 2013
You're right - corrected. (I wonder if it used to be closest, because I think Azzi's solution also does closest. I can't successfully run his because I don't have whatever toolbox fullfact() is in.)
Azzi Abdelmalek
le 27 Sep 2013
What I've understood from his question is to find the two points (Pn,Pm) with distance Pn-Pm closest to the distance P1-P2. The points are not necessary the closest. The function fullfact (statitistic toolbox) can be replaced by ndgrid function
Image Analyst
le 27 Sep 2013
I didn't think of that, but it could be. It's not really clear and specifically stated.
Azzi Abdelmalek
le 26 Sep 2013
Modifié(e) : Azzi Abdelmalek
le 27 Sep 2013
AA=[x ;y];
id=numel(x);
idx=unique(sort(fullfact([id id]),2),'rows');
idx(~diff(idx'),:)=[]
distance=sqrt((x(idx(:,1))-x(idx(:,2))).^2+(y(idx(:,1))-y(idx(:,2))).^2);
%----------------------------------------------------------------------
%p1 = (39,20) and p2 =(63,11)
x1=39;
y1=20;
x2=63;
y2=11;
dist1= sqrt((x1-x2)^2+(y1-y2)^2);
%----------------------------------------------------------------------
a=abs(distance-dist1);
[ii,jj]=sort(a);
id=jj(2);
point1=AA(:,idx(id,1))
point2=AA(:,idx(id,2))
%or
AA=[x ;y];
id=numel(x);
[ii,jj]=ndgrid(1:id,1:id);
idx=[jj(:) ii(:)]
idx=unique(sort(idx,2),'rows');
idx(~diff(idx'),:)=[]
distance=sqrt((x(idx(:,1))-x(idx(:,2))).^2+(y(idx(:,1))-y(idx(:,2))).^2);
%----------------------------------------------------------------------
%p1 = (39,20) and p2 =(63,11)
x1=39;
y1=20;
x2=63;
y2=11;
dist1= sqrt((x1-x2)^2+(y1-y2)^2);
%----------------------------------------------------------------------
a=abs(distance-dist1);
[ii,jj]=sort(a);
id=jj(2);
point1=AA(:,idx(id,1))
point2=AA(:,idx(id,2))
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!