how to find distance between two points?

조회 수: 2,213(최근 30일)
bsd 2011년 9월 27일
답변: Daksh 2023년 2월 2일
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.

채택된 답변

Lucas García
Lucas García 2011년 9월 27일
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 =
  댓글 수: 3
zoher badr
zoher badr 2021년 2월 14일
in this case you have to have a nested loop so you gaurantee to reach each point int the list

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

추가 답변(6개)

MathWorks Support Team
MathWorks Support Team 2018년 11월 8일
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
Advik Solanki
Advik Solanki 2022년 2월 28일

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

Walter Roberson
Walter Roberson 2011년 9월 27일
No. You will have to code it yourself.
There are many different possible meanings for "distance". See

Fangjun Jiang
Fangjun Jiang 2011년 9월 27일
Pos=[x1 x2;y1 y2]
  댓글 수: 4
Fangjun Jiang
Fangjun Jiang 2011년 9월 27일
I am using my new MATLAB version today. It has a bunch of toolbox. Nice!

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

Twinkle Jain
Twinkle Jain 2017년 3월 17일
X = [0,0;2,1];
d = pdist(X,'euclidean')

Sohrab Dorodvand
Sohrab Dorodvand 2018년 8월 2일
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?

Daksh 2023년 2월 2일
I understand you're experiencing doubts over calculating distance between 2 points in the figure for which you have variable values saved in workspace. You can use one of the following methods for your utility:
  1. norm(): distance between two points as the norm of the difference between the vector elements
  2. pdist(X): Euclidean distance between pairs of observations in X
  3. pdist2(X,Y,Distance): distance between each pair of observations in X and Y using the metric specified by Distance.
  4. distance(): distance between two points in Geographic space
Hope this helps!


Find more on Get Started with MATLAB in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by