在matlab中从邻接矩阵创建图

在matlab中从邻接矩阵创建图

本文介绍了在matlab中从邻接矩阵创建图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在matlab中有一个邻接矩阵。我如何绘制图表?因为我有> 500个节点,所以我不能使用随机(或网格状)坐标的gplot。因此,假设你有的其他文档和示例。

  %%受影响的线条:
set(h.nodes(nn),'Color',[0.2 0.8 0.2] );
fowEdges = getedgesbynodeid(h,get(h.Nodes(nn),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(nn)),'ID'));
edges = [fowEdges; revEdges];
set(边缘,'LineColor',[0.2 0.8 0.2])
set(边缘,'LineWidth',1.5)

%%颜色故障线:
set(h.nodes(newFaultNodes),'Color',[1 0.4 0.4]);
fowEdges = getedgesbynodeid(h,get(h.Nodes(newFaultNodes),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(newFaultNodes)),'ID'));
edges = [fowEdges; revEdges];
set(边缘,'LineColor',[1 0 0])
set(边缘,'LineWidth',2)


I have an adjacency matrix in matlab. How do I draw its graph? As I have >500 nodes, I cannot use gplot with random (or grid-like) coordinates.

解决方案

So assuming you have the bioinformatics toolbox, the biograph function is perfect for what you want to do.

Here's what I have done in the past:

Suppose from and to are two vectors containing information regarding the to-from nodes in the system. Then you can create your adjacency matrix this way:

Sys = sparse(from,to,1,s,s);
Adj_mat = tril(Sys + Sys');

I assume you already have your adjacency matrix adj_mat, in which case all you have to do is the following:

bg = biograph(Adj_mat,[],'ShowArrows','off','ShowWeights','off');
h = view(bg);

I added a few arguments just to illustrate some possibilities. I you want arrows, skip that part.

The nodes will by default be placed according to a "minimum energy" criterion, meaning that the number of "branch crossings" is minimized. The layout is possible to change, adding for instance 'LayoutType','Radial'.

Check out the documentation for more information.

Here is an example I have created using biograph earlier. It's simple to make the edges straight, or assign different color and weights to the branches. Also, you can create different names to the nodes, or remove the names and just have a "dot" there instead.

EDIT

As an answer to Vincent's question in the comments:

I used set(edges,'LineWidth',1.5) to set the edge weight. Check out some additional documentation and examples here.

%% Color affected lines:
set(h.nodes(nn),'Color',[0.2 0.8 0.2]);
fowEdges = getedgesbynodeid(h,get(h.Nodes(nn),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(nn)),'ID'));
edges = [fowEdges;revEdges];
set(edges,'LineColor',[0.2 0.8 0.2])
set(edges,'LineWidth',1.5)

%% Color faulted line:
set(h.nodes(newFaultNodes),'Color',[1 0.4 0.4]);
fowEdges = getedgesbynodeid(h,get(h.Nodes(newFaultNodes),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(newFaultNodes)),'ID'));
edges = [fowEdges;revEdges];
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',2)

这篇关于在matlab中从邻接矩阵创建图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 04:56