calculate the euclidean distance among all the pairs of nodes and use those resulting distances as the edges weights of the graph
조회 수: 1 (최근 30일)
이전 댓글 표시
Given the positions of nodes in a graph, i.e. x and y coordinates of each node, is there any compact way to
- calculate the euclidean distance among all the pairs of nodes
- and use those resulting distances as the edges weights of the graph ?
% input
s = [1 1 1 2 3 3 4 4 5 6 7 6 8 9 10 10 12 12 13 14 15 16 17 17 18 19 20 21 20 25];
t = [2 8 18 3 4 23 5 21 6 7 8 11 9 10 11 12 14 13 15 18 16 17 18 25 19 20 1 22 24 26];
G = graph(s,t);
x = [0.5 0 0 0 0.5 1 1.5 2 3 3 3 5.5 6 4 6 6 4 3 2 0.5 -1 -2 -1 1.5 4.5 4.5];
y = [0 0.5 1 1.5 2 2 1.5 1 1 1.5 2 1 0.5 0.5 0 -1 -1 -0.5 -1 -1 1 0.5 0.5 -0.5 -0.5 0];
plot(G,'XData',x,'YData',y,'linewidth',2,'MarkerSize',7);
>> G
G =
graph with properties:
Edges: [30×1 table]
Nodes: [26×0 table]
% Example of distance between node 1 and node 2:
d_12 = sqrt( (0.5-0)^2 + (-0.5+0)^2 )
댓글 수: 0
채택된 답변
Matt J
2023년 1월 24일
This is already done in spatialgraph2d()
which I think you are familiar with.
% input
s = [1 1 1 2 3 3 4 4 5 6 7 6 8 9 10 10 12 12 13 14 15 16 17 17 18 19 20 21 20 25];
t = [2 8 18 3 4 23 5 21 6 7 8 11 9 10 11 12 14 13 15 18 16 17 18 25 19 20 1 22 24 26];
G = graph(s,t);
x = [0.5 0 0 0 0.5 1 1.5 2 3 3 3 5.5 6 4 6 6 4 3 2 0.5 -1 -2 -1 1.5 4.5 4.5];
y = [0 0.5 1 1.5 2 2 1.5 1 1 1.5 2 1 0.5 0.5 0 -1 -1 -0.5 -1 -1 1 0.5 0.5 -0.5 -0.5 0];
obj=spatialgraph2D(G,x,y);
obj.G.Edges
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!