matlab adjacency list to adjacency matrix

前端 未结 2 875
执笔经年
执笔经年 2021-01-07 17:28

How to convert adjacency list to adjacency matrix via matab

For example: Here is the adjacency list(undirected), the third column is the weight.

1 2 3

2条回答
  •  感情败类
    2021-01-07 17:59

    It's really hard to tell what your'e asking. Is this right?

    list = [1 2 3
    1 3 4
    1 4 5
    2 3 4
    2 5 8
    2 4 7];
    
    
    matrix = zeros(max(max(list(:, 1:2))));  %// Or just zeros(5) if you know you want a 5x5 result
    
    matrix(sub2ind(size(matrix), list(:,1), list(:,2))) = list(:,3);  %// Populate the upper half
    matrix = matrix + matrix'  %'// Find the lower half via symmetry
    
    matrix =
    
       0   3   4   5   0
       3   0   4   7   8
       4   4   0   0   0
       5   7   0   0   0
       0   8   0   0   0
    

提交回复
热议问题