function Q=modularity_index(A,T)
% A newman spectral algorithm adapted from the brain connectivity toolbox. 
% Original code in here  https://sites.google.com/site/bctnet/measures/list 


N=length(A);      %number of vertices
K=sum(A);         %degree
m=sum(K);         %number of edges (each undirected edge is counted twice)
B=A-(K.'*K)/m;    %modularity matrix

if size(T,1)==1
    T=T';
end
s=T(:,ones(1,N));                      %compute modularity
Q=~(s-s.').*B/m;
Q=sum(Q(:));
 
end