Clear Filters
Clear Filters

how to convert edge list to adjacency matrix

5 views (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 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.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!