Convert Adjacency list of graph into Adjacency matrix
3 次查看(过去 30 天)
显示 更早的评论
回答(3 个)
Steven Lord
2018-10-17
How is your data stored?
Do you have a matrix or two vectors containing the endpoints of the edges? In this case you can use sparse or accumarray as Bruno suggested.
Is your data stored in a different way? If so, describe your storage system and we may be able to offer some guidance.
Guillaume
2018-10-17
One possible way to read your file:
lists = strsplit(fileread('list.txt'), {'\r', '\n'});
if isempty(lists{end}), lists = lists(1:end-1); end
lists = cellfun(@(v) sscanf(v, '%d'), lists, 'UniformOutput', false);
adj = sparse(repelem(1:numel(lists), cellfun('length', lists)), vertcat(lists{:}), 1)
If you want a full matrix:
full(adj)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!