How to make complete graph from co-ordinates.
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ashish Verma
il 12 Ago 2021
Commentato: Walter Roberson
il 12 Ago 2021
x=[0.3 ,5.6 , -8.4,6.4 ] % These are my x-cordinates
y=[4.6, 6.9,3.6,7.89]% These are my y-cordinates
I want to plot complete undirected graph with edge weight in between nodes.
0 Commenti
Risposta accettata
Chunru
il 12 Ago 2021
Modificato: Chunru
il 12 Ago 2021
Show the node name, edge weight/distance, and node coordinates
x=[0.3 ,5.6 , -8.4,6.4 ]; % These are my x-cordinates
y=[4.6, 6.9,3.6,7.89]; % These are my y-cordinates
n = length(x);
g = graph(ones(n,n), 'omitselfloops'); % complete graph
g.Edges.Weight = zeros(size(g.Edges.EndNodes, 1),1);
% edge distance
for i=1:size(g.Edges, 1)
g.Edges.Weight(i) = norm([x(g.Edges.EndNodes(i,2))-x(g.Edges.EndNodes(i,1)) ...
y(g.Edges.EndNodes(i,2))-y(g.Edges.EndNodes(i,1))]);
end
g.Nodes.Name=["A", "B", "C", "D"]'; % node name
%g.Edges.Weight = randi([20,90], [6,1]); % weigth %[1:6]'
h = plot(g, 'NodeLabel',g.Nodes.Name,'EdgeLabel',g.Edges.Weight);
h.XData = x;
h.YData = y;
2 Commenti
Più risposte (3)
Walter Roberson
il 12 Ago 2021
x=[0.3 ,5.6 , -8.4,6.4 ] % These are my x-cordinates
y=[4.6, 6.9,3.6,7.89]% These are my y-cordinates
nx = length(x);
CG = ones(nx, nx);
CG(1:nx+1:end) = 0;
G = graph(CG)
plot(G, 'XData', x, 'YData', y)
5 Commenti
Walter Roberson
il 12 Ago 2021
x =[0.3 ,5.6 , -8.4,6.4 ] % These are my x-cordinates
y =[4.6, 6.9,3.6,7.89]% These are my y-cordinates
dists = squareform(pdist([x(:), y(:)]));
[s, t, w] = find(dists);
G = graph(s, t, round(w,2));
G.Nodes.Names = {'A', 'B', 'C', 'D'}.';
plot(G, 'XData', x, 'YData', y, 'EdgeLabel', G.Edges.Weight, 'NodeLabel', G.Nodes.Names);
Vedere anche
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!