0

我一直在尝试标记无向图的边缘,而且,我正在使用这个 matgraph 工具!我成功地制作了一个图表,我只是想给它分配权重......请帮忙!

这是我尝试过的,

clear all;
close all;
clc;
g=graph;
for k=1:6

add(g,k,k+1)
add(g,1,4)
add(g,5,7)
end
ndraw(g);
x=rand(1,1);
y=rand(1,1)
A =[0 x 0 x 0 0 0;
x 0 x 0 0 0 0;
0 x 0 x 0 0 0;
x 0 x 0 x 0 0;
0 0 0 y 0 x x;
0 0 0 0 x 0 x;
0 0 0 0 x x 0]

在此处输入图像描述

4

1 回答 1

1

如果我理解正确,您可以在您编写的代码之后添加此代码:

% get line info from the figure
lineH = findobj(gca, 'type', 'line');
xData = cell2mat(get(lineH, 'xdata')); % get x-data
yData = cell2mat(get(lineH, 'ydata')); % get y-data

% if an edge is between (x1,y1)<->(x2,y2), place a label at
% the center of the line, i.e. (x1+x2)/2 (y1+y2)/2 etc
labelposx=mean(xData');
labelposy=mean(yData');

% generate some random weights vector
weights=randi(21,length(labelposx),1); 

% plot the weights on top of the figure
text(labelposx,labelposy,mat2cell(weights), 'HorizontalAlignment','center',... 
                                            'BackgroundColor',[.7 .9 .7]); 

在此处输入图像描述

于 2012-10-26T07:10:41.293 回答