How to draw networks in Matlab?

亡梦爱人 提交于 2019-12-17 10:03:35

问题


I have a matrix A in Matlab of dimension mx2 that contains in each row the labels of two nodes showing a direct link in a network, e.g.:

if the network has 4 nodes the matrix A could be A=[1 2; 1 3; 2 1; 2 4; 3 2; 4 1; 4 2], where the first row means that there is a link from 1 to 2, the second row means that there is a link from 1 to 3, etc.

Could you suggest me a quick way to draw the network from A?


回答1:


If you want the links to be directional, and have the Bioinformatics toolbox, you can create a biograph object. This also allows for labelling the nodes with identification strings if you so desire, see the help file. If not they'll be called "Node 1", "Node 2", etc. You'll need to convert your list of links to an adjacency matrix - @RTL gave the accumarray version, you can also use 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



回答2:


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',[])




回答3:


Alternative solution using builtin gplot function

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)

for large networks the adjacency matrix can be generated as sparse with accumarray(A,1,[],[],0,true)




回答4:


MatLab > R2015b now have the Graph and Network Algorithms.

A is what you would call an edge list.

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

Will draw the network.




回答5:


You may also be interested in Matgraph, Matlab toolbox for graph manipulation:

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



来源:https://stackoverflow.com/questions/23804335/how-to-draw-networks-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!