forked from ivanbrugere/matlab-networks-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inc2edgeL.m
executable file
·31 lines (24 loc) · 999 Bytes
/
inc2edgeL.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
%##################################################################
% Convert an incidence matrix to an edge list.
%
% Inputs: inc - incidence matrix nxm (number of nodes x number of edges)
% Outputs: edge list - mx3, m x (node 1, node 2, edge weight)
%
% Example: [-1; 1] <=> [1,2,1], one directed (1->2) edge
% GB: last updated, Sep 25 2012
%##################################################################
function el = inc2edgeL(inc)
m = size(inc,2); % number of edges
el = zeros(m,3); % initialize edge list [n1, n2, weight]
for e=1:m
ind_m1 = find(inc(:,e)==-1);
ind_p1 = find(inc(:,e)==1);
if numel(ind_m1)==0 & numel(ind_p1)==1 % undirected, self-loop
el(e,:) = [ind_p1 ind_p1 1];
elseif numel(ind_m1)==0 & numel(ind_p1)==2 % undirected
el(e,:) = [ind_p1(1) ind_p1(2) 1];
el=[el; ind_p1(2) ind_p1(1) 1];
elseif numel(ind_m1)==1 & numel(ind_p1)==1 % directed
el(e,:) = [ind_m1 ind_p1 1];
end
end