Distance between points in a graph

11 Ansichten (letzte 30 Tage)
Varun Shrishail Soraganvi
Varun Shrishail Soraganvi am 23 Jan. 2022
Bearbeitet: Walter Roberson am 24 Jan. 2022
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 :)

Akzeptierte Antwort

Image Analyst
Image Analyst am 23 Jan. 2022
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)
distances = 5×5
0 0.5189 0.3179 0.6963 0.7453 0.5189 0 0.3392 0.7892 0.5037 0.3179 0.3392 0 0.8942 0.7670 0.6963 0.7892 0.8942 0 0.4612 0.7453 0.5037 0.7670 0.4612 0
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.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 23 Jan. 2022
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 Kommentare
Varun Shrishail Soraganvi
Varun Shrishail Soraganvi am 23 Jan. 2022
Bearbeitet: Varun Shrishail Soraganvi am 23 Jan. 2022
The coordinates that I am getting are in a 5*2 matrix. I am trying to find distance from the matrix. At first I thought I will try and do it using the graph, but matrix seems easier. Trying to figure out a way. Tried pdist(), but could you please tell me what do you mean by "configured with a number of different distance metrics"?
Walter Roberson
Walter Roberson am 24 Jan. 2022
Bearbeitet: Walter Roberson am 24 Jan. 2022
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_cheb = 5×5
0 0.7191 0.6385 0.1655 0.7087 0.7191 0 0.8544 0.5536 0.1438 0.6385 0.8544 0 0.7660 0.8440 0.1655 0.5536 0.7660 0 0.5432 0.7087 0.1438 0.8440 0.5432 0
distances_city = squareform(pdist(xy, 'cityblock'))
distances_city = 5×5
0 0.7639 0.7737 0.2930 0.8973 0.7639 0 1.5376 0.6363 0.1543 0.7737 1.5376 0 1.0668 1.6710 0.2930 0.6363 1.0668 0 0.6043 0.8973 0.1543 1.6710 0.6043 0
distances_euc = squareform(pdist(xy, 'euclidean'))
distances_euc = 5×5
0 0.7205 0.6527 0.2089 0.7334 0.7205 0 1.0940 0.5597 0.1442 0.6527 1.0940 0 0.8229 1.1817 0.2089 0.5597 0.8229 0 0.5466 0.7334 0.1442 1.1817 0.5466 0
distances_ma = squareform(pdist(xy, 'mahalanobis'))
distances_ma = 5×5
0 2.2872 2.2800 0.4380 1.9505 2.2872 0 2.2957 2.0834 0.5991 2.2800 2.2957 0 2.5133 2.5543 0.4380 2.0834 2.5133 0 1.6623 1.9505 0.5991 2.5543 1.6623 0
distances_mi = squareform(pdist(xy, 'minkowski', 3))
distances_mi = 5×5
0 0.7192 0.6405 0.1876 0.7131 0.7192 0 0.9805 0.5542 0.1439 0.6405 0.9805 0 0.7811 1.0528 0.1876 0.5542 0.7811 0 0.5434 0.7131 0.1439 1.0528 0.5434 0
... and others

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by