【问题标题】:How to draw networks in Matlab?Matlab中如何画网络?
【发布时间】:2014-07-11 08:15:16
【问题描述】:

我在 Matlab 中有一个矩阵 A,维度为 mx2,每行包含两个节点的标签,显示网络中的直接链接,例如:

如果网络有4 节点,则矩阵A 可能是A=[1 2; 1 3; 2 1; 2 4; 3 2; 4 1; 4 2],其中第一行表示存在从12 的链接,第二行表示存在链接从13

您能建议我一种从 A 绘制网络的快速方法吗?

【问题讨论】:

标签: matlab graph plot graph-theory graph-visualization


【解决方案1】:

MatLab > R2015b 现在有Graph and Network Algorithms

A 就是你所说的边缘列表。

plot(digraph(A(:,1),A(:,2))

将绘制网络。

【讨论】:

    【解决方案2】:

    您可能还对 Matgraph,用于图形操作的 Matlab 工具箱感兴趣:

    http://www.mathworks.com/matlabcentral/fileexchange/19218-matgraph

    【讨论】:

      【解决方案3】:

      如果您希望链接是定向的,并且拥有生物信息学工具箱,您可以创建一个biograph 对象。如果您愿意,这也允许使用标识字符串标记节点,请参阅帮助文件。如果不是,它们将被称为“节点 1”、“节点 2”等。您需要将链接列表转换为邻接矩阵 - @RTL 提供了 accumarray 版本,您也可以使用 sub2ind:

      N = 4;
      adj = zeros(N);
      adj(sub2ind([N,N], A(:,1),A(:,2))) = 1;
      
      bg = biograph(adj);  % make biograph object
      dolayout(bg);   % automatically calculate positions for nodes
      view(bg); % what it says on the tin
      

      【讨论】:

        【解决方案4】:

        使用内置gplot 函数的替代解决方案

        adj=accumarray(A,1)
        n=size(adj,1); % number of nodes
        coord=[cos((1:n).'*(2*pi/n)),sin((1:n).'*(2*pi/n))] % points on a circle for nodes
        gplot(adj,coord)
        

        对于大型网络,邻接矩阵可以用accumarray(A,1,[],[],0,true) 生成为稀疏矩阵

        【讨论】:

          【解决方案5】:
          n = max(A(:)); %// number of nodes
          theta = linspace(0,2*pi,n+1); %// the nodes will be on a circle
          theta = theta(1:end-1);
          x = cos(theta); %// x coordinates of nodes
          y = sin(theta); %// y coordinates of nodes
          plot(x, y, 'ro') %// plot nodes
          hold on
          plot(x(A).', y(A).', 'b-') %// plot edges
          axis image
          axis(1.2*[-1 1 -1 1])
          set(gca,'xtick',[],'ytick',[])
          

          【讨论】:

          • 可以加链接的方向吗?
          • 您可以使用annotation 代替plot。问题是annotation使用了归一化轴单位,比较麻烦
          猜你喜欢
          • 1970-01-01
          • 2013-12-10
          • 2014-04-03
          • 2012-07-10
          • 2015-12-03
          • 2011-08-23
          • 2013-07-11
          • 2013-02-01
          • 2012-05-05
          相关资源
          最近更新 更多