distance_bin.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. function D=distance_bin(A)
  2. %DISTANCE_BIN Distance matrix
  3. %
  4. % D = distance_bin(A);
  5. %
  6. % The distance matrix contains lengths of shortest paths between all
  7. % pairs of nodes. An entry (u,v) represents the length of shortest path
  8. % from node u to node v. The average shortest path length is the
  9. % characteristic path length of the network.
  10. %
  11. % Input: A, binary directed/undirected connection matrix
  12. %
  13. % Output: D, distance matrix
  14. %
  15. % Notes:
  16. % Lengths between disconnected nodes are set to Inf.
  17. % Lengths on the main diagonal are set to 0.
  18. %
  19. % Algorithm: Algebraic shortest paths.
  20. %
  21. %
  22. % Mika Rubinov, U Cambridge
  23. % Jonathan Clayden, UCL
  24. % 2007-2013
  25. % Modification history:
  26. % 2007: Original (MR)
  27. % 2013: Bug fix, enforce zero distance for self-connections (JC)
  28. A=double(A~=0); %binarize and convert to double format
  29. l=1; %path length
  30. Lpath=A; %matrix of paths l
  31. D=A; %distance matrix
  32. Idx=true;
  33. while any(Idx(:))
  34. l=l+1;
  35. Lpath=Lpath*A;
  36. Idx=(Lpath~=0)&(D==0);
  37. D(Idx)=l;
  38. end
  39. D(~D)=inf; %assign inf to disconnected nodes
  40. D(1:length(A)+1:end)=0; %clear diagonal