calculate the euclidean distance among all the pairs of nodes and use those resulting distances as the edges weights of the graph

9 visualizzazioni (ultimi 30 giorni)
Given the positions of nodes in a graph, i.e. x and y coordinates of each node, is there any compact way to
  1. calculate the euclidean distance among all the pairs of nodes
  2. 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 )

Risposta accettata

Matt J
Matt J il 24 Gen 2023
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
ans = 30×2 table
EndNodes Weight ________ _______ 1 2 0.70711 1 8 1.8028 1 18 2.5495 1 20 1 2 3 0.5 3 4 0.5 3 23 1.118 4 5 0.70711 4 21 1.118 5 6 0.5 6 7 0.70711 6 11 2 7 8 0.70711 8 9 1 9 10 0.5 10 11 0.5

Più risposte (0)

Categorie

Scopri di più su Graph and Network Algorithms in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by