Matlab图论重量分配

时间:2012-10-26 06:21:51

标签: matlab graph-theory

我一直试图标记无向图的边缘,我也在使用这个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]

enter image description here

1 个答案:

答案 0 :(得分: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]); 

enter image description here