Distance between points in a graph
26 ビュー (過去 30 日間)
古いコメントを表示
Varun Shrishail Soraganvi
2022 年 1 月 23 日
編集済み: Walter Roberson
2022 年 1 月 24 日
I have plotted a graph which has 5 points. Is there any in built feature in Matlab which can help me calculate the distance between each point? Any help is appreciated, Thank you :)
0 件のコメント
採用された回答
Image Analyst
2022 年 1 月 23 日
I your points are in a matrix called xy, with each point being a row, and you want to find the distance of every point to every other points, you can use pdist2() in the Statistics and Machine Learning Toolbox:
xy = rand(5, 2); % Sample points.
% Compute distances between all points:
distances = pdist2(xy, xy)
The row is one point, and in each column are the distances to the other points, so for example, distances(4, 2) is the distance from the point in row 4 to the point in row 2. Of course this will be the same distance as distances(2, 4) and the diagonal will be zero since the distance of any point to itself is zero.
その他の回答 (1 件)
Walter Roberson
2022 年 1 月 23 日
pdist() can be configured with a number of different distance metrics.
However, I have to wonder if you are assuming that the 5 points are on a curve, and if you want the distance along the curve. If so then we would need to know what model you are using for the curve joining the points.
2 件のコメント
Walter Roberson
2022 年 1 月 24 日
編集済み: Walter Roberson
2022 年 1 月 24 日
You asked for "distance", but you did not specify how distance was to be calculated
xy = rand(5, 2); % Sample points.
distances_cheb = squareform(pdist(xy, 'chebychev'))
distances_city = squareform(pdist(xy, 'cityblock'))
distances_euc = squareform(pdist(xy, 'euclidean'))
distances_ma = squareform(pdist(xy, 'mahalanobis'))
distances_mi = squareform(pdist(xy, 'minkowski', 3))
... and others
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!