edges to vertices distance
7 views (last 30 days)
Show older comments
Hello respected reader I want to ask a question. I want to get a matrix like distance matrix of vertices. I want to get distance matrices between edges and vertices.. please help me.
5 Comments
Matt J
on 28 Jun 2020
OP's comment moved here:
like I have a path graph label vertices with v1, v2, ...,vn and I want to measure distance from edge one that is v1v2 to vertex vn. like this... for any simple indirected graph.
Answers (2)
Image Analyst
on 28 Jun 2020
Try pdist2()
distances = pdist2(v, v);
where v is an N rows - by - 2 column matrix of N (x,y) coordinates:
[v1x, v1y;
v2x, v2y;
v3x, v3y;
etc...........
2 Comments
Image Analyst
on 28 Jun 2020
Edited: Image Analyst
on 28 Jun 2020
It's in the Statistics and Machine learning Toolbox. If you don't have it, you can simply use a double nested for loop to compute the same thing.
v = randi(9, 5, 2); % Sample data
[rows, columns] = size(v);
distances = zeros(rows, rows);
for k1 = 1 : rows
for k2 = 1 : rows
% Use Euclidean distance formula.
distances(k2, k1) = sqrt((v(k1, 1) - v(k2, 1)) ^ 2 + (v(k1, 2) - v(k2, 2)) ^ 2);
end
end
distances % Show in command window
distances =
0 1.0000 6.0000 6.3246 2.2361
1.0000 0 5.0000 5.3852 2.8284
6.0000 5.0000 0 2.0000 7.2801
6.3246 5.3852 2.0000 0 8.0623
2.2361 2.8284 7.2801 8.0623 0
The i, jth element is the distance from (vix, viy) to (vjx, vjy).
Steven Lord
on 28 Jun 2020
If you've built a graph or digraph object, call distances to get the distances between each pairs of nodes or shortestpath if you just need the shortest distance between one specific pair. Some of the other functions on this documentation page may be of use in further analysis on the graph or digraph object.
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!