Azzera filtri
Azzera filtri

how to convert edge list to adjacency matrix

5 visualizzazioni (ultimi 30 giorni)
muhammad ismat
muhammad ismat il 18 Feb 2016
Commentato: muhammad ismat il 20 Feb 2016
i have the code
function adj=edgeL2adjj(e)
Av=[e; fliplr(e)];
nodes=unique(Av(:, 1:2)); % get all nodes, sorted
adj=zeros(numel(nodes)); % initialize adjacency matrix
% across all edges
for i=1:size(Av,1)
adj(nodes==Av(i,1),(nodes==Av(i,2)))=1;
end
end
in matlab to convert edge list to adjacency matrix but if i input u=[8 5;1 4;3 5;6 7] then i divided into two set[8 5;1 4], [3 5,6 7] and apply previous code on [8 5;1 4] will get matrix 7 x 7 but i want 8 x 8

Risposte (1)

Walter Roberson
Walter Roberson il 19 Feb 2016
u=[8 5;1 4;3 5;6 7];
num_nodes = max(u(:));
adj_matrix = accumarray(u, 1, [num_nodes, num_nodes]);
Note: if there can be duplicate entries you can add ">= 1" to the end.

Categorie

Scopri di più su Data Type Conversion 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