How can I calculate the distance between a set of points and a single point within a matrix?
61 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Stephanie Diaz
am 11 Jun. 2017
Kommentiert: fatemeh zare
am 4 Apr. 2021
Hi,
I need to calculate the euclidean distance between a set of points on a matrix, and one other point in the same matrix. I and J are 9x1 vectors, where I represents the "x" and J represents "y" coordinates of a set of 9 points. "rows" and "columns" are the x and y coordinates of a single point. I have the following code:
for g=1:length(I)
for f=2
I(g)=x2
J(f)=y2
distance_between_points = sqrt((x2 - rows)^2 + (y2 - columns)^2);
end
end
I obtain a single value from this, where I should be getting a distance value between each point represented by I,J,and the point represent by (rows, columns). I've tried pdist2 and I get ~30 values which makes a little less sense. Is there a way to do this?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 11 Jun. 2017
Bearbeitet: Image Analyst
am 11 Jun. 2017
If x and y are the vectors of all the coordinates, and xp and xp are the coordinates of the single point, then you can find all the distances between (xp, yp) and all the other points doing this:
distances = sqrt((x-xp).^2 + (y-yp).^2);
No for loop(s) needed.
By the way, don't confuse x with rows and y with columns like you did in your code. x is columns, not rows. y is rows, not columns. Arrays are indexed m(y, x) and NOT as m(x,y)!
2 Kommentare
Weitere Antworten (1)
Walter Roberson
am 11 Jun. 2017
Without a loop:
distance_between_points = sqrt((I - columns).^2 + (J - rows).^2)
Notice that columns is an x coordinate, not a y coordinate
Siehe auch
Kategorien
Mehr zu NaNs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!