Finding determinant of matrix using Lower triangular matrix

152 Views Asked by At

I have algorithms for finding determinant of matrix and pre-transforming it to upper triangular matrix form:

clear all;
A = rand([3 3])*100;
B = A;
N = size(A);
for K = 1 : N-1
    for I = K+1 : N
        for J = K:N
            if J == K
                aik = A(I,K);
            end
            A(I,J) = (A(I,J) - aik/A(K,K)*A(K,J));
        end
    end
end
det = 1;
for I = 1:N
    det = det*A(I,I);
end

This code works very well. Now I would like to develop a similar algorithm, but for the case of a lower triangular matrix. How I can do that? Importantly, I need to use MATLAB without calling any external functions.

1

There are 1 best solutions below

1
Сергей Корягин On BEST ANSWER

Correct solution:

clear all;
A = rand([5 5])*100;
det(A);
B = A;
N = size(A);
for K = N:-1: 1
    for I = K-1:-1: 1
        for J = N:-1:1
            if J == K
                aik = A(I,K);
            end
            A(I,J) = (A(I,J) - aik/A(K,K)*A(K,J));
        end
    end
end
det = 1;
for I = N:-1:1
    det = det*A(I,I);
end