关联矩阵 Matlab 的图表

Graph out of incidence matrix Matlab

我需要的恰恰相反;

I = incidence(G).

这里我已经有一个关联矩阵I,有什么办法可以得到G,这是一个包含节点和边的图?

我没有找到 Matlab 方法,但我开发的以下方法非常简单:

% Define your test graph
s = [1 1 1 2 3 3];
t = [2 3 4 3 4 5];
G = graph(s,t);
I = incidence(G);

% Find the source nodes from incidence matrix
[s2,~] = find( I == -1 )

% Find the target nodes from incidence matrix
[t2,~] = find( I == 1 )

% Generate graph from source and target nodes
G2 = graph( s2, t2 );
figure;
subplot(211);
plot( G );
subplot(212);
plot( G2 );

% Check
I2 = incidence(G2);

assert( isequal(I, I2), 'Did not generate same incidence matrices' );

所以所有的工作都是用 find 和信息完成的:

I = incidence(G) returns the sparse incidence matrix for graph G. If s and t are the node IDs of the source and target nodes of the jth edge in G, then I(s,j) = -1 and I(t,j) = 1. That is, each column of I indicates the source and target nodes for a single edge in G.