How to label all the edges in a graph the euclidean distance between two adjacent nodes???

조회 수: 1 (최근 30일)
Hi,
I have to weight each edge in below attached undirected graph with euclidean distance between two adjacent nodes. How we can do that???

채택된 답변

Akira Agata
Akira Agata 2019년 3월 18일
I think one possible solution would be like this:
% Make Graph object
rng('default');
s = [1 1 3];
t = [2 3 4];
G = graph(s,t);
% Calculate pair-wise Euclidian distance between every node
X = 10*rand(4,1);
Y = 10*rand(4,1);
d = pdist([X,Y]);
d = squareform(d);
% Label Euclidian distance for each edge
G.Edges.Wieght =...
arrayfun(@(j,k) d(j,k),G.Edges.EndNodes(:,1),G.Edges.EndNodes(:,2));
% Visualize the Graph
plot(G,'XData',X,'YData',Y,'EdgeLabel',G.Edges.Wieght)
graph.png
  댓글 수: 2
Mohammad Bhat
Mohammad Bhat 2019년 3월 18일
Thank you sir, for your reply. Can it be done with Graph_theory toolbox:-
________________________________________________________
function W = build_euclidean_weight_matrix(A,vertex,pad_with)
% build_euclidean_weight_matrix - build a weighted adjacenty matrix
% that reflect position of the vertices.
% The weight is Inf if vertices are not connected.
% You can put something different from Inf (e.g. 0) 'using pad_with'.
%
% W = build_euclidean_weight_matrix(A,vertex [,pad_with] );
%
% Copyright (c) 2004 Gabriel Peyr?
if nargin<2
error('Not enough arguments.');
end
if nargin<3
pad_with = Inf;
end
if size(vertex,1)<size(vertex,2)
vertex = vertex';
end
n = size(A,1);
if size(vertex,1)<n
error('Not enough vertices');
end
W = sparse(n,n);
W(:,:) = pad_with;
for i = 1:n
for j=1:n
if A(i,j)~=0
W(i,j) = norm( vertex(i,:)-vertex(j,:), 'fro' );
end
end
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by