MATLAB Answers

bsd

how to find distance between two points?

Asked by bsd
on 27 Sep 2011

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

  0 Comments

Tags

Products

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 =
    2.2361

  1 Comment

manish sharma on 30 Jun 2012

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 http://en.wikipedia.org/wiki/Metric_%28mathematics%29#Examples

  0 Comments


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

  4 Comments

Fangjun Jiang
on 27 Sep 2011

@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.
pos=rand(2,5)
D=dist(pos)

Fangjun Jiang
on 27 Sep 2011

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

Fangjun Jiang
on 27 Sep 2011

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


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today