-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathedgeL2adj.m
executable file
·36 lines (29 loc) · 958 Bytes
/
edgeL2adj.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
% Convert edge list to adjacency matrix.
%
% INPUTS: edge list: mx3, m - number of edges
% OUTPUTS: adjacency matrix nxn, n - number of nodes
%
% Note: information about nodes is lost: indices only (i1,...in) remain
% Last updated: Sep 25, 2012
function adj = edgeL2adj(el)
nodes = sort(unique([el(:, 1) el(:, 2)])); % get all nodes, sorted
adj = zeros(numel(nodes)); % initialize adjacency matrix
% across all edges
for i = 1:size(el, 1)
adj(find(nodes == el(i, 1)), find(nodes == el(i, 2))) = el(i, 3);
end
%!test
%!shared T
%! T = load_test_graphs();
%! for i=1:length(T)
%! if not(strcmp( T{i}{3}, 'adjacency' )); continue; end
%! edgeL = T{i}{5};
%! % adding 1s to get the expected edge list dimensions right
%! if size(edgeL)(2)==2
%! edgeL = [edgeL ones(size(edgeL)(1),1)];
%! end
%! assert(T{i}{2}, edgeL2adj( edgeL ))
%! end
%!demo
%! edgeL2adj([1 2 1])
%! edgeL2adj([1 1 1; 2 3 1])