how to find distance between two points?
古いコメントを表示
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
採用された回答
その他の回答 (6 件)
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.2361
2 件のコメント
Peize Li
2020 年 12 月 30 日
Will i get a column vector of distances if I try norm(x-y), where x and y are two 3 x 2 vectors?
Advik Solanki
2022 年 2 月 28 日
thanks
Walter Roberson
2011 年 9 月 27 日
6 投票
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
2011 年 9 月 27 日
Pos=[x1 x2;y1 y2]
D=dist(Pos);
4 件のコメント
Walter Roberson
2011 年 9 月 27 日
Which "dist" function are you referring to, Fangjun ? The only one I find is in the Neural Networks toolbox, and it returns a matrix of distances rather than a single distance.
http://www.mathworks.com/help/toolbox/nnet/ref/dist.html
Fangjun Jiang
2011 年 9 月 27 日
@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
2011 年 9 月 27 日
Sorry, Walter. You are right, the dist() function is from the Neural Network Toolbox.
Fangjun Jiang
2011 年 9 月 27 日
I am using my new MATLAB version today. It has a bunch of toolbox. Nice!
Twinkle Jain
2017 年 3 月 17 日
X = [0,0;2,1];
d = pdist(X,'euclidean')
Sohrab Dorodvand
2018 年 8 月 2 日
0 投票
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 日
0 投票
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:
- norm(): distance between two points as the norm of the difference between the vector elements
- pdist(X): Euclidean distance between pairs of observations in X
- pdist2(X,Y,Distance): distance between each pair of observations in X and Y using the metric specified by Distance.
- distance(): distance between two points in Geographic space
Hope this helps!
カテゴリ
ヘルプ センター および File Exchange で Manage Products についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!