function V=mnhf_invu(U) %MNHF_INVU inverts an upper triangular matrix % % U - the (square, upper triangular) matrix in question % % Usage mnhf_invu(U); [r,~] = size(U); % Compute the size of U. V = size(U); for i=2:r V(i,1:i-1) = 0.0; % Elements below main diagonal. end for i=1:r V(i,i)=1.0/U(i,i); % Diagonal elements. end for j=2:r for i=j-1:-1:1 % Elements above main diagonal. V(i,j) = -1.0/U(i,i)*sum(U(i,i+1:j)*V(i+1:j,j)); end end