function [U,D] = sorteig(M)
%function [U,D] = sorteig(M)
%routine to automatically sort eigenvalues/eigenvectors in decreasing order

[m,m] = size(M);

[TU,TD] = eig(M);

D = zeros(m,m);


[junk,order] = sort(-diag(TD));		% negative to get reverse sort

for i=1:m
  U(:,i) = TU(:,order(i));   %index(i));
  D(i,i) = TD(order(i),order(i)); %vr(i);
end

