Network distances

From InterSciWiki

Jump to: navigation, search


Matlab program for network distances by Haifeng Du. If you copy and paste directly into the command menu you get "Error: Function definitions are not permitted at the prompt or in scripts." Copy instead into the the "New m-file" opened at the upper left icon (blank white with red tab upper left). First save the file (possibly under a new name). The press "Debug" and "Run." Then reopen the icon for matlab and the result should be there. You may have to close other m-file windows that are open in over to save under a previous name. WikiSysopWikiSysop 09:49, 2 February 2008 (PST)

function main
clear all; clc;
M=rand(20,20)>0.5;
M=(M+M')>1;
for i=1:10
   M(i,i)=0;
end
   
distM=distanceM(M)
%-------------------------------------------------------------------------
% this subfunction can be used to get the distance of the symmetrical 0-1
% networks
% Input: the 0-1 network M;
% Output: the distance matrix distM;
% Hiafeng and Doug
%-------------------------------------------------------------------------
function distM=distanceM(M)
       N=size(M,1);
       tempM=(M+M')>=1;
       [I,J]=find(sum(tempM)~=0);
       SM=M(J,J');
       SM=SM*1;
       N1=size(SM,1);
       for i=1:N1
          SM(i,i)=1; 
       end
       D=SM;
       i=1;
       p=1;
       temp1=SM;
       while (~isempty(p)) & (i<=N1)
           i=i+1;
           temp1=temp1*SM;
           temp=temp1.*(D<1);
           [p,q]=find(temp~=0);
           for k=1:size(p)
               D(p(k),q(k))=i;    
           end
           [p,q]=find(D~=0);
           temp1=temp1>=1;
       end


       for i=1:N1
          D(i,i)=0; 
       end
       
       distM=D;
Personal tools