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 CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by