function D = degree_matrix(A) n = size(A,1); D = zeros(n,n); for i=1:n for j=1:n if i ~= j D(i,i) = D(i,i) + A(i,j); end end end