Calculating the modularity matrix