Convert edge list to adjacency matrix

38 ビュー (過去 30 日間)
muhammad ismat
muhammad ismat 2015 年 7 月 29 日
回答済み: Steven Lord 2015 年 7 月 29 日
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]

採用された回答

Steven Lord
Steven Lord 2015 年 7 月 29 日
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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by