-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadjMatrixToList.m
executable file
·35 lines (31 loc) · 1015 Bytes
/
adjMatrixToList.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
function List = adjMatrixToList(A, listForLinksIn)
%% Convert the adjacency matrix A to an adjacency list of who the connected nodes are,
% for connections into each node iff listForLinksIn == true, else
% for connections out of each node iff listForLinksIn == false.
% Node pair is considered connected iff A(i,j) ~= 0
%
% Input:
% - A - 2D matrix, A(i,j) means a link i->j
% - listForLinksIn (optional) - boolean, whether to list connections into (true) or
% out from each node
%
% Output:
% - List - cell array, where A(n) is a vector of the connected node ids for n
%
%% Linear Sync Toolkit (linsync)
% Copyright (C) 2023 Joseph T. Lizier
% Distributed under GNU General Public License v3
if (nargin < 2)
listForLinksIn = false;
else
List = cell(length(A),1);
for n=1:length(A)
if (listForLinksIn)
% Pull out the incoming connections for n
List{n} = find(A(:,n) > 0)';
else
% Pull out the outgoing connections for n
List{n} = find(A(n,:) > 0);
end
end
end