Matlab function to compute average neighbor degree

孤街醉人 提交于 2019-12-04 15:01:55

Even for large edge list, you can use Matlab to create an adjacency matrix that fits into memory using sparse matrix:

el = [2 1; 3 1; ... ]; %// edge list, I put only a tiny sample here...
n = max( el(:) ); %// number of nodes in the graph
A = sparse( el(:,1), el(:,2), 1, n, n ); % //sparse adjacency matrix

The neighbor degree of each node is the number of neighbors

nd = sum( A, 2 ); %// degree of each node

To compute the average neighbor degree, one can construct another sparse matrix with the neighbor degree stored in each entry

ndM = sparse( el(:,1), el(:,2), nd( el(:,2) ), n, n ); 

The average neighbor degree can now be computed from the new matrix

av = full( sum( ndM, 2 ) ./ nd );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!