how to find distance between two points?

2,397 views (last 30 days)
bsd
bsd on 27 Sep 2011
Commented: Advik Solanki on 28 Feb 2022
Hai,
I need to find the distance between two points in the figure, which I have plotted. Is there any function in matlab that could find the distance between two points. Looking for your reply.
BSD

Accepted Answer

Lucas García
Lucas García on 27 Sep 2011
You can use the pdist function in the Statistics Toolbox:
e.g: distance between points (0,0) and (2,1)
>> X = [0,0;2,1];
>> d = pdist(X,'euclidean')
d =
2.2361
  3 Comments
zoher badr
zoher badr on 14 Feb 2021
in this case you have to have a nested loop so you gaurantee to reach each point int the list

Sign in to comment.

More Answers (7)

MathWorks Support Team
MathWorks Support Team on 8 Nov 2018
The distance between two points x and y is the same as the magnitude of the vector that points from one point to the other:
>> x = [0 0];
>> y = [2 1];
>> norm(x-y)
ans =
2.2361
  2 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 27 Sep 2011
No. You will have to code it yourself.
There are many different possible meanings for "distance". See http://en.wikipedia.org/wiki/Metric_%28mathematics%29#Examples

Fangjun Jiang
Fangjun Jiang on 27 Sep 2011
Pos=[x1 x2;y1 y2]
D=dist(Pos);
  4 Comments
Fangjun Jiang
Fangjun Jiang on 27 Sep 2011
I am using my new MATLAB version today. It has a bunch of toolbox. Nice!

Sign in to comment.


Twinkle Jain
Twinkle Jain on 17 Mar 2017
X = [0,0;2,1];
d = pdist(X,'euclidean')

Sohrab Dorodvand
Sohrab Dorodvand on 2 Aug 2018
if i was to compare one point of a 1d graph and to compare the distances between that point(the reference point) to others on the graph. how can i do that?

Johannes Hüffmeier
Johannes Hüffmeier on 6 May 2020
Is therre a way to take in features like a timestamp for evaluation? I would like to get the closest point in time and space.

adel odeh
adel odeh on 4 Jun 2021
Write a MATLAB code that calculate the distance between two points X(1,-3,-2) and Y(3,1,2).
  1 Comment
Walter Roberson
Walter Roberson on 5 Jun 2021
It is meaningless to talk about "the" distance between two points, especially when the units of measure are not given.
There are many different distance measures. For example https://en.wikipedia.org/wiki/Minkowski_distance or the Infinity Norm https://en.wikipedia.org/wiki/Uniform_norm

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!