Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# lu

LU factorization

## Syntax

[L,U] = lu(A)
[L,U,P] = lu(A)
[L,U,p] = lu(A,'vector')
[L,U,p,q] = lu(A,'vector')
[L,U,P,Q,R] = lu(A)
[L,U,p,q,R] = lu(A,'vector')
lu(A)

## Description

[L,U] = lu(A) returns an upper triangular matrix U and a matrix L, such that A = L*U. Here, L is a product of the inverse of the permutation matrix and a lower triangular matrix.

[L,U,P] = lu(A) returns an upper triangular matrix U, a lower triangular matrix L, and a permutation matrix P, such that P*A = L*U.

[L,U,p] = lu(A,'vector') returns the permutation information as a vector p, such that A(p,:) = L*U.

[L,U,p,q] = lu(A,'vector') returns the permutation information as two row vectors p and q, such that A(p,q) = L*U.

[L,U,P,Q,R] = lu(A) returns an upper triangular matrix U, a lower triangular matrix L, permutation matrices P and Q, and a scaling matrix R, such that P*(R\A)*Q = L*U.

[L,U,p,q,R] = lu(A,'vector') returns the permutation information in two row vectors p and q, such that R(:,p)\A(:,q) = L*U.

lu(A) returns the matrix that contains the strictly lower triangular matrix L (the matrix without its unit diagonal) and the upper triangular matrix U as submatrices. Thus, lu(A) returns the matrix U + L - eye(size(A)), where L and U are defined as [L,U,P] = lu(A). The matrix A must be square.

## Input Arguments

 A Square or rectangular symbolic matrix. 'vector' Flag that prompts lu to return the permutation information in row vectors.

## Output Arguments

 L Lower triangular matrix or a product of the inverse of the permutation matrix and a lower triangular matrix. U Upper triangular matrix. P Permutation matrix. p Row vector. q Row vector. Q Permutation matrix. R Diagonal scaling matrix.

## Examples

Compute the LU factorization of this matrix. Because these numbers are not symbolic objects, you get floating-point results.

`[L, U] = lu([2 -3 -1; 1/2 1 -1; 0 1 -1])`
```L =
1.0000         0         0
0.2500    1.0000         0
0    0.5714    1.0000

U =
2.0000   -3.0000   -1.0000
0    1.7500   -0.7500
0         0   -0.5714```

Now convert this matrix to a symbolic object, and compute the LU factorization:

`[L, U] = lu(sym([2 -3 -1; 1/2 1 -1; 0 1 -1]))`
```L =
[   1,   0, 0]
[ 1/4,   1, 0]
[   0, 4/7, 1]

U =
[ 2,  -3,   -1]
[ 0, 7/4, -3/4]
[ 0,   0, -4/7]```

Compute the LU factorization returning the lower and upper triangular matrices and the permutation matrix:

```syms a;
[L, U, P] = lu(sym([0 0 a; a 2 3; 0 a 2]))```
```L =
[ 1, 0, 0]
[ 0, 1, 0]
[ 0, 0, 1]

U =
[ a, 2, 3]
[ 0, a, 2]
[ 0, 0, a]

P =
[ 0, 1, 0]
[ 0, 0, 1]
[ 1, 0, 0]```

Use the 'vector' flag to return the permutation information as a vector:

```syms a;
A = [0 0 a; a 2 3; 0 a 2];
[L, U, p] = lu(A, 'vector')```
```L =
[ 1, 0, 0]
[ 0, 1, 0]
[ 0, 0, 1]

U =
[ a, 2, 3]
[ 0, a, 2]
[ 0, 0, a]

p =
[ 2, 3, 1]```

Use isAlways to check that A(p,:) = L*U:

`isAlways(A(p,:) == L*U)`
```ans =
1     1     1
1     1     1
1     1     1```

Restore the permutation matrix P from the vector p:

```P = zeros(3, 3);
for i = 1:3
P(i, p(i)) = 1;
end;
P```
```P =
0     1     0
0     0     1
1     0     0```

Compute the LU factorization of this matrix returning the permutation information in the form of two vectors p and q:

```syms a
A = [a, 2, 3*a; 2*a, 3, 4*a; 4*a, 5, 6*a];
[L, U, p, q] = lu(A, 'vector')```
```L =
[ 1, 0, 0]
[ 2, 1, 0]
[ 4, 3, 1]

U =
[ a,  2,  3*a]
[ 0, -1, -2*a]
[ 0,  0,    0]

p =
[ 1, 2, 3]

q =
[ 1, 2, 3]```

Use isAlways to check that A(p, q) = L*U:

`isAlways(A(p, q) == L*U)`
```ans =
1     1     1
1     1     1
1     1     1```

Compute the LU factorization of this matrix returning the lower and upper triangular matrices, permutation matrices, and the scaling matrix:

```syms a;
A = [0, a; 1/a, 0; 0, 1/5; 0,-1];
[L, U, P, Q, R] = lu(A)```
```L =
[ 1,       0, 0, 0]
[ 0,       1, 0, 0]
[ 0, 1/(5*a), 1, 0]
[ 0,    -1/a, 0, 1]

U =
[ 1/a, 0]
[   0, a]
[   0, 0]
[   0, 0]

P =
[ 0, 1, 0, 0]
[ 1, 0, 0, 0]
[ 0, 0, 1, 0]
[ 0, 0, 0, 1]

Q =
[ 1, 0]
[ 0, 1]

R =
[ 1, 0, 0, 0]
[ 0, 1, 0, 0]
[ 0, 0, 1, 0]
[ 0, 0, 0, 1]```

Use isAlways to check that P*(R\A)*Q = L*U:

`isAlways(P*(R\A)*Q == L*U)`
```ans =
1     1
1     1
1     1
1     1```

Compute the LU factorization of this matrix using the 'vector' flag to return the permutation information as vectors p and q. Also compute the scaling matrix R:

```syms a;
A = [0, a; 1/a, 0; 0, 1/5; 0,-1];
[L, U, p, q, R] = lu(A, 'vector')```
```L =
[ 1,       0, 0, 0]
[ 0,       1, 0, 0]
[ 0, 1/(5*a), 1, 0]
[ 0,    -1/a, 0, 1]

U =
[ 1/a, 0]
[   0, a]
[   0, 0]
[   0, 0]

p =
[ 2, 1, 3, 4]

q =
[ 1, 2]

R =
[ 1, 0, 0, 0]
[ 0, 1, 0, 0]
[ 0, 0, 1, 0]
[ 0, 0, 0, 1]```

Use isAlways to check that R(:,p)\A(:,q) = L*U:

`isAlways(R(:,p)\A(:,q) == L*U)`
```ans =
1     1
1     1
1     1
1     1```

Call the lu function for this matrix:

```syms a;
A = [0 0 a; a 2 3; 0 a 2];
lu(A)```
```ans =
[ a, 2, 3]
[ 0, a, 2]
[ 0, 0, a]```

Verify that the resulting matrix is equal to U + L - eye(size(A)), where L and U are defined as [L,U,P] = lu(A):

```[L,U,P] = lu(A);
U + L - eye(size(A))```
```ans =
[ a, 2, 3]
[ 0, a, 2]
[ 0, 0, a]```

expand all

### LU Factorization of a Matrix

LU factorization expresses an m-by-n matrix A as P* A = L *U. Here, L is an m-by-m lower triangular matrix, U is an m-by-n upper triangular matrix, and P is a permutation matrix.

### Permutation Vector

Permutation vector p contains numbers corresponding to row exchanges in the matrix A. For an m-by-m matrix, p represents the following permutation matrix with indices i and j ranging from 1 to m:

### Tips

• Calling lu for numeric arguments that are not symbolic objects invokes the MATLAB® lu function.

• The thresh option supported by the MATLAB lu function does not affect symbolic inputs.

• If you use 'matrix' instead of 'vector', then lu returns permutation matrices, as it does by default.

• L and U are nonsingular if and only if A is nonsingular. lu also can compute the LU factorization of a singular matrix A. In this case, L or U is a singular matrix.

• Most algorithms for computing LU factorization are variants of Gaussian elimination.