how to convert edge list to adjacency matrix

5 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2016-2-19
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.

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by