How to make complete graph from co-ordinates.

조회 수: 9 (최근 30일)
Ashish Verma
Ashish Verma 2021년 8월 12일
댓글: Walter Roberson 2021년 8월 12일
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.

채택된 답변

Chunru
Chunru 2021년 8월 12일
편집: Chunru 2021년 8월 12일
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
Ashish Verma
Ashish Verma 2021년 8월 12일
Thank sir, but i want to show weight of edges too and instead of node 1,2,3,4 i want to give some name like 'A', 'B' and so on.
Chunru
Chunru 2021년 8월 12일
Those numbers are weight not node number. Weight are some random number now.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Walter Roberson
Walter Roberson 2021년 8월 12일
x=[0.3 ,5.6 , -8.4,6.4 ] % These are my x-cordinates
x = 1×4
0.3000 5.6000 -8.4000 6.4000
y=[4.6, 6.9,3.6,7.89]% These are my y-cordinates
y = 1×4
4.6000 6.9000 3.6000 7.8900
nx = length(x);
CG = ones(nx, nx);
CG(1:nx+1:end) = 0;
G = graph(CG)
G =
graph with properties: Edges: [6×2 table] Nodes: [4×0 table]
plot(G, 'XData', x, 'YData', y)
  댓글 수: 5
Ashish Verma
Ashish Verma 2021년 8월 12일
If co-ordintes are given then the distance between the points are fixed,we can not asign any random distances between the nodes.
Walter Roberson
Walter Roberson 2021년 8월 12일
x =[0.3 ,5.6 , -8.4,6.4 ] % These are my x-cordinates
x = 1×4
0.3000 5.6000 -8.4000 6.4000
y =[4.6, 6.9,3.6,7.89]% These are my y-cordinates
y = 1×4
4.6000 6.9000 3.6000 7.8900
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);

댓글을 달려면 로그인하십시오.


Ashish Verma
Ashish Verma 2021년 8월 12일
Sir, sorry for saying weight i must say distances between the points.

Ashish Verma
Ashish Verma 2021년 8월 12일
Sir,i dont want to put any random weight. For example take two elements from x (0.3 , 5.6) and y(4.6 , 6.9) then distances between the points will be sqrt((4.6-0.3)^2 + (6.9-5.6)^2) = 4.49.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by