Convert edge list to adjacency matrix

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