如何使用MATLAB绘制邻接矩阵 [英] How to graph adjacency matrix using MATLAB
问题描述
我想创建一个显示相邻矩阵之间节点之间连接的图.
I want to create a plot showing connections between nodes from an adjacency matrix like the one below.
gplot 似乎是最好的工具.但是,为了使用它,我需要传递每个节点的坐标.问题是我不知道坐标应该在哪里,我希望该功能能够为我找到一个好的布局.
gplot seems like the best tool for this. However, in order to use it, I need to pass the coordinate of each node. The problem is that I don't know where the coordinates should be, I was hoping the function would be capable of figuring out a good layout for me.
例如,这是使用以下任意坐标的输出:
For example here's my output using the following arbitrary coordinates:
A = [1 1 0 0 1 0;
1 0 1 0 1 0;
0 1 0 1 0 0;
0 0 1 0 1 1;
1 1 0 1 0 0;
0 0 0 1 0 0];
crd = [0 1;
1 1;
2 1;
0 2;
1 2;
2 2];
gplot (A, crd, "o-");
很难读懂,但是如果我稍微玩弄一下坐标并将其更改为以下坐标,它将变得更加可读.
Which is hard to read, but if I play around with the coordinates a bit and change them to the following it becomes much more readable.
crd = [0.5 0;
0 1;
0 2;
1 2;
1 1;
1.5 2.5];
我不希望得到完美优化的坐标或其他任何东西,但是我该如何告诉MATLAB使用某种
I don't expect perfectly optimized coordinates or anything, but how can I tell MATLAB to automatically figure out a set of coordinates for me that looks okay using some sort of algorithm so I can graph something that looks like the top picture.
谢谢.
推荐答案
从R2015b开始,MATLAB现在具有一套图形和网络算法.对于此示例,您可以创建无向图对象,然后对其进行绘制使用已重载的plot
函数:
As of R2015b, MATLAB now has a suite of graph and network algorithms. For this example, you can create an undirected graph object and then plot it using the overloaded plot
function:
% Create symmetric adjacency matrix
A = [1 1 0 0 1 0;
1 0 1 0 1 0;
0 1 0 1 0 0;
0 0 1 0 1 1;
1 1 0 1 0 0;
0 0 0 1 0 0];
% Create undirected graph object
G = graph(A);
% Plot
plot(G);
这篇关于如何使用MATLAB绘制邻接矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!