Convert edge list to adjacency matrix

38 Ansichten (letzte 30 Tage)
muhammad ismat
muhammad ismat am 29 Jul. 2015
Beantwortet: Steven Lord am 29 Jul. 2015
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]

Akzeptierte Antwort

Steven Lord
Steven Lord am 29 Jul. 2015
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.

Weitere Antworten (0)

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by