Convert edge list to adjacency matrix

38 visualizzazioni (ultimi 30 giorni)
if i have the following code
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
that convert edge list m x 3 to adjacency list n x n but i have a matrix of edge list m x 2 so what is the required change in previous code that give me true result .
example if edge list =[1 2;2 3;2 4] then adjacency matrix=[0 1 0 0; 0 0 1 1; 0 0 0 0; 0 0 0 0]

Risposta accettata

Steven Lord
Steven Lord il 29 Lug 2015
If you want a sparse adjacency matrix, call SPARSE.
edgeList = [1 2; 2 3; 2 4];
adj = sparse(edgeList(:, 1), edgeList(:, 2), 1, 4, 4);
Note that if you have a repeated edge in your edgeList, the corresponding element of adj will be greater than 1.
If you want a full adjacency matrix, either convert the sparse adjacency matrix to FULL or call ACCUMARRAY.

Più risposte (0)

Categorie

Scopri di più su Sparse Matrices in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by