フィルターのクリア

how to convert edge list to adjacency matrix

5 ビュー (過去 30 日間)
muhammad ismat
muhammad ismat 2016 年 2 月 18 日
コメント済み: muhammad ismat 2016 年 2 月 20 日
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.
  1 件のコメント
muhammad ismat
muhammad ismat 2016 年 2 月 20 日
Thanks to your efforts, it's simple code

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by