forked from aeolianine/octave-networks-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathadj2inc.m
executable file
·52 lines (38 loc) · 1.37 KB
/
adj2inc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
%##################################################################
% Convert adjacency matrix to an incidence matrix
% Note: Valid for directed/undirected, simple/not simple graphs
%
% INPUTs: adjacency matrix, nxn
% OUTPUTs: incidence matrix: n x m (number of edges)
%
% Other routines used: isDirected.m
% GB: last updated, Sep 25 2012
%##################################################################
function inc = adj2inc(adj)
n=length(adj); % number of nodes
inc=[]; % initialize incidence matrix
if isDirected(adj)
for i=1:n
for j=1:n
% handle self-loops
if i==j; for x=1:adj(i,j); inc=[inc; zeros(1,length(1:i-1)), 1,zeros(1,length(i+1:n))]; end; continue; end
for x=1:adj(i,j) % add multiple edges if any
if i<j
inc=[inc; zeros(1,length(1:i-1)),-1,zeros(1,length(i+1:j-1)),1,zeros(1,length(j+1:n))];
else
inc=[inc; zeros(1,length(1:j-1)),1,zeros(1,length(j+1:i-1)),-1,zeros(1,length(i+1:n))];
end
end
end
end
else % undirected
for i=1:n
for j=i:n
% handle self-loops
if i==j; for x=1:adj(i,j); inc=[inc; zeros(1,length(1:i-1)), 1,zeros(1,length(i+1:n))]; end; continue; end
% add multiple edges if any
for x=1:adj(i,j); inc=[inc; zeros(1,length(1:i-1)),1,zeros(1,length(i+1:j-1)),1,zeros(1,length(j+1:n))]; end
end
end
end
inc=inc';