是否有方法從關聯矩陣繪製圖形? 按圖我的意思是http://en.wikipedia.org/wiki/Graph_(mathematics)不是情節。matlab從關聯矩陣中繪製圖形
到目前爲止,我只發現如何將關聯矩陣轉換爲鄰接矩陣。 在R中,這可能與igraph
庫有關。那麼,有沒有一種簡單的方法來做到這一點在MATLAB
是否有方法從關聯矩陣繪製圖形? 按圖我的意思是http://en.wikipedia.org/wiki/Graph_(mathematics)不是情節。matlab從關聯矩陣中繪製圖形
到目前爲止,我只發現如何將關聯矩陣轉換爲鄰接矩陣。 在R中,這可能與igraph
庫有關。那麼,有沒有一種簡單的方法來做到這一點在MATLAB
您可以使用gplot
:
k = 1:30;
[B,XY] = bucky;
gplot(B(k,k),XY(k,:),'-*')
axis square
此功能在機器學習問題,是常用的。在搜索過程中,我看到了一個implementation for weighted graph plotting。
http://www.mathworks.com/help/matlab/ref/gplot.html
編輯:
dt = 2*pi/10;
t = dt:dt:2*pi;
x = cos(t); y = sin(t);
A = ones(10);
gplot(A,[x' y']);
A = ones(3,3);
gplot(A,[x' y']);
a = [0 1 1; 1 0 0; 1 1 0];
gplot(a,[x' y'] ,'-*');
所有你需要做的就是確保XY
飛機有足夠的(X,Y)在你的圖中的每個節點對。
這裏是A的gplot
:
這裏是一個solutionthat使用矩陣喜歡你的一個
% Define a matrix A.
A = [0 1 1 0 ; 1 0 0 1 ; 1 0 0 1 ; 0 1 1 0];
% Draw a picture showing the connected nodes.
cla
subplot(1,2,1);
gplot(A,[0 1;1 1;0 0;1 0],'.-');
text([-0.2, 1.2 -0.2, 1.2],[1.2, 1.2, -.2, -.2],('1234')', ...
'HorizontalAlignment','center')
axis([-1 2 -1 2],'off')
% Draw a picture showing the adjacency matrix.
subplot(1,2,2);
xtemp=repmat(1:4,1,4);
ytemp=reshape(repmat(1:4,4,1),16,1)';
text(xtemp-.5,ytemp-.5,char('0'+A(:)),'HorizontalAlignment','center');
line([.25 0 0 .25 NaN 3.75 4 4 3.75],[0 0 4 4 NaN 0 0 4 4])
axis off tight
圖的帶箭頭
所有以前的答覆都是在不考慮圖表是否直接的情況下處理圖表。我的意思是,如果存在邊緣(i,j),則邊緣(j,i)可能不存在。要考慮到這一點,通過下面的代碼:
% This figure will be used to plot the structure of the graph represented
% by the current A matrix.
figure
dt = 2*pi/N_robots;
t = dt:dt:2*pi;
x = cos(t); y = sin(t);
agents=[ 2 2.5;
0.5 2.0;
0.5 1.0;
2.0 0.5;
3.5 1.0;
3.5 2.0;];
agents = p0;
agents = [x' y'];
% plot(agents(:,1),agents(:,2),'s','MarkerSize', 20, 'MarkerFaceColor', [1 0 1])
grid on
%xlim([0 4])
%ylim([0 3])
hold on
index=1;
% The following prints the non-directed graph corresponding to the A matrix
for i=1:N_robots
for j=index:N_robots
if A(i,j) == 1
arrowline(agents([i j],1),agents([i j],2),'arrowsize',600);
% plot(agents([i j],1),agents([i j],2),'--','LineWidth',2.5);
end
end
end
set(gca,'FontSize',fontsize2)
title('Structure of the Graph','interpreter', 'latex','FontSize', 18)
你可以有以下結果:
這是工作肯定6個代理商現在。我沒有時間去測試一般數量的代理,但原則上它應該可以工作。你可以使用不同的代理矢量來做到這一點。
我希望這會幫助你。