Constructing an Adjacency Matrix from a Matrix/Table

조회 수: 2 (최근 30일)
Mae
Mae 2016년 5월 2일
댓글: Steven Lord 2018년 7월 21일
I have a matrix, LinkData, whose first 2 columns contains node reference numbers, and the 3rd column contains data that the 2 nodes share. I am trying to create an Adjacency matrix using LinkData. I'm not even completely sure what an Adjacency matrix is let alone construct it. I know that it is supposed to identify which nodes are adjacent to each other (i.e. node 2 is adjacent to node 1 and 3, and node 3 is adjacent to node 2 and 4).
I have tried using accumarray but I do not think I got the right results.
nodesPairs = [LinkData(:,1),LinkData(:,2)] %extract node pairs;
accumarray(nodesPairs+1,1);
Any help would be deeply appreciated!

답변 (1개)

Walter Roberson
Walter Roberson 2016년 5월 2일
Close, but you should use
nodesPairs = [LinkData(:,1),LinkData(:,2)]; %extract node pairs
nodePairs = [nodePairs; fliplr(nodePairs)];
adj = accumarray(nodePairs+1, 1);
You should also give consideration to,
adj = sparse([LinkData(:,1); LinkData(:,2)]+1, [LinkData(:,2); LinkData(:,1)]+1, 1);
If you have a sufficiently recent MATLAB, then consider
G = graph(nodePairs(:,1), nodePairs(:,2));
adj = adjacency(G);
  댓글 수: 2
Mohammad Bhat
Mohammad Bhat 2018년 7월 21일
Then, how to plot the graph..
Steven Lord
Steven Lord 2018년 7월 21일
If you made a graph as per the last section of code in Walter's answer, just plot it.
plot(G)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by