MATLAB Answers


how to find distance between two points?

Asked by bsd
on 27 Sep 2011


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.





No products are associated with this question.

3 Answers

Answer by Lucas García
on 27 Sep 2011
 Accepted answer

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 =

  1 Comment

That worked for me. Thanks Lucas :)

Answer by Walter Roberson
on 27 Sep 2011

No. You will have to code it yourself.

There are many different possible meanings for "distance". See


Answer by Fangjun Jiang
on 27 Sep 2011
Pos=[x1 x2;y1 y2]


@Walter, just the dist() function in MATLAB, not associated to any particular Toolbox. help dist or doc dist will brings it up.
There are many call syntax of dist(). I though the OP wants the Euclidean distance between two points (x1,y1), (x2,y2), which should be sqrt((x1-x2)^2+(y1-y2)^2).
dist() can calculate the Euclidean distance of multiple points at once, it can certainly be used to calculate the distance for two points, although it seems to be an over-kill because the equation sqrt((x1-x2)^2+(y1-y2)^2) can do that too.
Since the OP asked for a MATLAB function, I thought this is the one.

Sorry, Walter. You are right, the dist() function is from the Neural Network Toolbox.

I am using my new MATLAB version today. It has a bunch of toolbox. Nice!

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!