How do you write MATLAB code for LU factorization when U is the unit matrix instead of L. The upper triangular matrix will have the diagonal of 1s instead of the lower triangular matrix.
CodePudding user response:
You are looking for Crout's Method for LU decomposition.
The Wikipedia article has the following code
function [L, U] = LUdecompCrout(A) [R, C] = size(A); for i = 1:R L(i, 1) = A(i, 1); U(i, i) = 1; end for j = 2:R U(1, j) = A(1, j) / L(1, 1); end for i = 2:R for j = 2:i L(i, j) = A(i, j) - L(i, 1:j - 1) * U(1:j - 1, j); end for j = i 1:R U(i, j) = (A(i, j) - L(i, 1:i - 1) * U(1:i - 1, j)) / L(i,i); end end end
CodePudding user response:
Here's one way to do this. If M is your matrix,
[P,Q] = lu(M');
L = Q'; U = P';
Alternatively, assuming that M has an LU decomposition, we could do the following:
[L,U,P,Q,D] = lu(M);
L = L*P'*D*Q';
This should yield a lower triangular L and upper triangular U where U has 1's on the diagonal whenever M has an LU decomposition.