## how to find distance between two points?

on 27 Sep 2011

### Lucas García (view profile)

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

## Products

No products are associated with this question.

### Lucas García (view profile)

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`

manish sharma

### manish sharma (view profile)

on 30 Jun 2012

That worked for me. Thanks Lucas :)

### Walter Roberson (view profile)

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 (view profile)

on 27 Sep 2011
```Pos=[x1 x2;y1 y2]
D=dist(Pos);
```

Fangjun Jiang

### Fangjun Jiang (view profile)

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

### Fangjun Jiang (view profile)

on 27 Sep 2011

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

Fangjun Jiang

### Fangjun Jiang (view profile)

on 27 Sep 2011

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

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