2012-10-26 23 views
0

我一直在試圖標記無向圖的邊緣,另外,我用這matgraph工具!我成功地製作了一個圖表,我只想給它分配權重......請幫助!Matlab的+圖論+重量分配

這裏是我試過,

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

如果我理解正確的話,你可以,你寫的代碼後添加以下代碼:

% 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

+0

謝謝內特!自從一天以來,我一直在努力做到這一點......我對matlab非常陌生,並且想知道,如何獲得一個相鄰的矩陣?! – happyme

+0

寫大寫字母被認爲是不禮貌的! 矩陣(g)返回圖的鄰接矩陣。這將返回一個方形邏輯矩陣。要在算術上使用此矩陣,請將其轉換爲double類;例如,下面的命令返回(的鄰接矩陣)的曲線圖的特徵值: EIG給定一個正方形,對稱的,零一,零對角矩陣A,(雙(矩陣(G))) 相反,我們可以設置g將這個矩陣作爲它的鄰接矩陣,如下所示:set_matrix(g,A)。另見間諜,拉普拉斯和發病率矩陣。 (見http://www.ams.jhu.edu/~ers/matgraph/matgraph.pdf) – bla

+0

哦,我很抱歉用大寫字母寫,無知我! nate,非常感謝你的幫助!這有助於很多!非常感謝! – happyme