matlab从关联矩阵绘制图形

时间:2012-11-09 13:51:01

标签: matlab graph plot gplots

有没有办法从Incidence矩阵中绘制图形。 通过图表我的意思是http://en.wikipedia.org/wiki/Graph_(mathematics)而不是情节。

到目前为止,我只发现如何将关联矩阵转换为邻接矩阵。 在R中,可以使用igraph库。那么在matlab中有一种简单的方法吗

3 个答案:

答案 0 :(得分:6)

您可以使用gplot

k = 1:30;
[B,XY] = bucky;
gplot(B(k,k),XY(k,:),'-*')
axis square

此功能通常用于机器学习问题。在搜索时,我看到了implementation for weighted graph plotting

enter image description here 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

enter image description here

答案 1 :(得分:3)

带箭头的图表

以前的所有回复都在处理图表,而不考虑图表是否是直接的。我的意思是,如果有一条边(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)

您可以获得以下结果: enter image description here

目前这对6个代理商来说肯定有用。我没有时间测试通用数量的代理,但原则上它应该有效。您可以使用不同的代理向量来执行此操作。

我希望这会对你有所帮助。

答案 2 :(得分:2)

这是一个使用像

这样的矩阵的解决方案
% 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

取自关于巴基的例子 http://www.mathworks.nl/products/matlab/examples.html;jsessionid=59c5cf7261bdf09589f79bab2bc2?file=/products/demos/shipping/matlab/buckydem.html