qr

QR factorization

Description

example

R = qr(A) returns the R part of the QR decomposition A = Q*R. Here, A is an m-by-n matrix, R is an m-by-n upper triangular matrix, and Q is an m-by-m unitary matrix.

example

[Q,R] = qr(A) returns an upper triangular matrix R and a unitary matrix Q, such that A = Q*R.

example

[Q,R,P] = qr(A) returns an upper triangular matrix R, a unitary matrix Q, and a permutation matrix P, such that A*P = Q*R. If all elements of A can be approximated by the floating-point numbers, then this syntax chooses the column permutation P so that abs(diag(R)) is decreasing. Otherwise, it returns P = eye(n).

example

[C,R] = qr(A,B) returns an upper triangular matrix R and a matrix C, such that C = Q'*B and A = Q*R. Here, A and B must have the same number of rows.

C and R represent the solution of the matrix equation A*X = B as X = R\C.

example

[C,R,P] = qr(A,B) returns an upper triangular matrix R, a matrix C, such that C = Q'*B, and a permutation matrix P, such that A*P = Q*R. If all elements of A can be approximated by the floating-point numbers, then this syntax chooses the permutation matrix P so that abs(diag(R)) is decreasing. Otherwise, it returns P = eye(n). Here, A and B must have the same number of rows.

C, R, and P represent the solution of the matrix equation A*X = B as X = P*(R\C).

example

[Q,R,p] = qr(A,'vector') returns the permutation information as a vector p, such that A(:,p) = Q*R.

example

[C,R,p] = qr(A,B,'vector') returns the permutation information as a vector p.

C, R, and p represent the solution of the matrix equation A*X = B as X(p,:) = R\C.

example

___ = qr(___,'econ') returns the "economy size" decomposition. If A is an m-by-n matrix with m > n, then qr computes only the first n columns of Q and the first n rows of R. For m <= n, the syntaxes with 'econ' are equivalent to the corresponding syntaxes without 'econ'.

When you use 'econ', qr always returns the permutation information as a vector p.

You can use 0 instead of 'econ'. For example, [Q,R] = qr(A,0) is equivalent to [Q,R] = qr(A,'econ').

example

___ = qr(___,'real') assumes that input arguments and intermediate results are real, and therefore, suppresses calls to abs and conj. When you use this flag, qr assumes that all symbolic variables represent real numbers. When using this flag, ensure that all numeric arguments are real numbers.

Use 'real' to avoid complex conjugates in the result.

Examples

R part of QR Factorization

Compute the R part of the QR decomposition of the 4-by-4 Wilkinson's eigenvalue test matrix.

Create the 4-by-4 Wilkinson's eigenvalue test matrix:

A = sym(wilkinson(4))
A =
[ 3/2,   1,   0,   0]
[   1, 1/2,   1,   0]
[   0,   1, 1/2,   1]
[   0,   0,   1, 3/2]

Use the syntax with one output argument to return the R part of the QR decomposition without returning the Q part:

R = qr(A)
R =
[ 13^(1/2)/2,        (4*13^(1/2))/13,            (2*13^(1/2))/13,                              0]
[          0, (13^(1/2)*53^(1/2))/26, (10*13^(1/2)*53^(1/2))/689,       (2*13^(1/2)*53^(1/2))/53]
[          0,                      0,   (53^(1/2)*381^(1/2))/106, (172*53^(1/2)*381^(1/2))/20193]
[          0,                      0,                          0,             (35*381^(1/2))/762]

QR Factorization of Pascal Matrix

Compute the QR decomposition of the 3-by-3 Pascal matrix.

Create the 3-by-3 Pascal matrix:

A = sym(pascal(3))
A =
[ 1, 1, 1]
[ 1, 2, 3]
[ 1, 3, 6]

Find the Q and R matrices representing the QR decomposition of A:

[Q,R] = qr(A)
Q =
[ 3^(1/2)/3, -2^(1/2)/2,  6^(1/2)/6]
[ 3^(1/2)/3,          0, -6^(1/2)/3]
[ 3^(1/2)/3,  2^(1/2)/2,  6^(1/2)/6]
 
R =
[ 3^(1/2), 2*3^(1/2), (10*3^(1/2))/3]
[       0,   2^(1/2),  (5*2^(1/2))/2]
[       0,         0,      6^(1/2)/6]

Verify that A = Q*R using isAlways:

isAlways(A == Q*R)
ans =
  3×3 logical array
     1     1     1
     1     1     1
     1     1     1

Permutation Information

Using permutations helps increase numerical stability of the QR factorization for floating-point matrices. The qr function returns permutation information either as a matrix or as a vector.

Set the number of significant decimal digits, used for variable-precision arithmetic, to 10. Approximate the 3-by-3 symbolic Hilbert matrix by floating-point numbers:

previoussetting = digits(10);
A = vpa(hilb(3))
A =
[          1.0,          0.5, 0.3333333333]
[          0.5, 0.3333333333,         0.25]
[ 0.3333333333,         0.25,          0.2]

First, compute the QR decomposition of A without permutations:

[Q,R] = qr(A)
Q =
[ 0.8571428571, -0.5016049166,  0.1170411472]
[ 0.4285714286,  0.5684855721, -0.7022468832]
[ 0.2857142857,  0.6520863915,  0.7022468832]
 
R =
[ 1.166666667, 0.6428571429,           0.45]
[           0, 0.1017143303,   0.1053370325]
[           0,            0, 0.003901371573]

Compute the difference between A and Q*R. The computed Q and R matrices do not strictly satisfy the equality A*P = Q*R because of the round-off errors.

A - Q*R
ans =
[ -1.387778781e-16, -3.989863995e-16, -2.064320936e-16]
[ -3.469446952e-18, -8.847089727e-17, -1.084202172e-16]
[ -2.602085214e-18, -6.591949209e-17, -6.678685383e-17]

To increase numerical stability of the QR decomposition, use permutations by specifying the syntax with three output arguments. For matrices that do not contain symbolic variables, expressions, or functions, this syntax triggers pivoting, so that abs(diag(R)) in the returned matrix R is decreasing.

[Q,R,P] = qr(A)
Q =
[ 0.8571428571, -0.4969293466, -0.1355261854]
[ 0.4285714286,  0.5421047417,  0.7228063223]
[ 0.2857142857,  0.6776309272, -0.6776309272]
R =
[ 1.166666667,         0.45,   0.6428571429]
[           0, 0.1054092553,   0.1016446391]
[           0,            0, 0.003764616262]
P =
     1     0     0
     0     0     1
     0     1     0

Check the equality A*P = Q*R again. QR factorization with permutations results in smaller round-off errors.

A*P - Q*R
ans =
[ -3.469446952e-18, -4.33680869e-18, -6.938893904e-18]
[                0, -8.67361738e-19, -1.734723476e-18]
[                0, -4.33680869e-19, -1.734723476e-18]

Now, return the permutation information as a vector by using the 'vector' argument:

[Q,R,p] = qr(A,'vector')
Q =
[ 0.8571428571, -0.4969293466, -0.1355261854]
[ 0.4285714286,  0.5421047417,  0.7228063223]
[ 0.2857142857,  0.6776309272, -0.6776309272]
R =
[ 1.166666667,         0.45,   0.6428571429]
[           0, 0.1054092553,   0.1016446391]
[           0,            0, 0.003764616262]
p =
     1     3     2

Verify that A(:,p) = Q*R:

A(:,p) - Q*R
ans =
[ -3.469446952e-18, -4.33680869e-18, -6.938893904e-18]
[                0, -8.67361738e-19, -1.734723476e-18]
[                0, -4.33680869e-19, -1.734723476e-18]

Exact symbolic computations let you avoid roundoff errors:

A = sym(hilb(3));
[Q,R] = qr(A);
A - Q*R
ans =
[ 0, 0, 0]
[ 0, 0, 0]
[ 0, 0, 0]

Restore the number of significant decimal digits to its default setting:

digits(previoussetting)

Use QR Decomposition to Solve Matrix Equation

You can use qr to solve systems of equations in a matrix form.

Suppose you need to solve the system of equations A*X = b, where A and b are the following matrix and vector:

A = sym(invhilb(5))
b = sym([1:5]')
A =
[    25,   -300,    1050,   -1400,    630]
[  -300,   4800,  -18900,   26880, -12600]
[  1050, -18900,   79380, -117600,  56700]
[ -1400,  26880, -117600,  179200, -88200]
[   630, -12600,   56700,  -88200,  44100]
b =
 
 1
 2
 3
 4
 5

Use qr to find matrices C and R, such that C = Q'*B and A = Q*R:

[C,R] = qr(A,b);

Compute the solution X:

X = R\C
X =
        5
    71/20
   197/70
  657/280
 1271/630

Verify that X is the solution of the system A*X = b using isAlways:

isAlways(A*X == b)
ans =
  5×1 logical array
     1
     1
     1
     1
     1

Use QR Decomposition with Permutation Information to Solve Matrix Equation

When solving systems of equations that contain floating-point numbers, use QR decomposition with the permutation matrix or vector.

Suppose you need to solve the system of equations A*X = b, where A and b are the following matrix and vector:

previoussetting = digits(10);
A = vpa([2 -3 -1; 1 1 -1; 0 1 -1]);
b = vpa([2; 0; -1]);

Use qr to find matrices C and R, such that C = Q'*B and A = Q*R:

[C,R,P] = qr(A,b)
C =
  -2.110579412
 -0.2132007164
  0.7071067812
R =
[ 3.31662479, 0.3015113446, -1.507556723]
[          0,  1.705605731, -1.492405014]
[          0,            0, 0.7071067812]
P =
     0     0     1
     1     0     0
     0     1     0

Compute the solution X:

X = P*(R\C)
X =
   1.0
 -0.25
  0.75

Alternatively, return the permutation information as a vector:

[C,R,p] = qr(A,b,'vector')
C =
  -2.110579412
 -0.2132007164
  0.7071067812
R =
[ 3.31662479, 0.3015113446, -1.507556723]
[          0,  1.705605731, -1.492405014]
[          0,            0, 0.7071067812]
p =
     2     3     1

In this case, compute the solution X as follows:

X(p,:) = R\C
X =
   1.0
 -0.25
  0.75

Restore the number of significant decimal digits to its default setting:

digits(previoussetting)

"Economy Size" Decomposition

Use 'econ' to compute the “economy size” QR decomposition.

Create a matrix that consists of the first two columns of the 4-by-4 Pascal matrix:

A = sym(pascal(4));
A = A(:,1:2)
A =
[ 1, 1]
[ 1, 2]
[ 1, 3]
[ 1, 4]

Compute the QR decomposition for this matrix:

[Q,R] = qr(A)
Q =
[ 1/2, -(3*5^(1/2))/10,    (3^(1/2)*10^(1/2))/10,          0]
[ 1/2,     -5^(1/2)/10, -(2*3^(1/2)*10^(1/2))/15,  6^(1/2)/6]
[ 1/2,      5^(1/2)/10,   -(3^(1/2)*10^(1/2))/30, -6^(1/2)/3]
[ 1/2,  (3*5^(1/2))/10,    (3^(1/2)*10^(1/2))/15,  6^(1/2)/6]
 
R =
[ 2,       5]
[ 0, 5^(1/2)]
[ 0,       0]
[ 0,       0]

Now, compute the “economy size” QR decomposition for this matrix. Because the number of rows exceeds the number of columns, qr computes only the first 2 columns of Q and the first 2 rows of R.

[Q,R] = qr(A,'econ')
Q =
[ 1/2, -(3*5^(1/2))/10]
[ 1/2,     -5^(1/2)/10]
[ 1/2,      5^(1/2)/10]
[ 1/2,  (3*5^(1/2))/10]
 
R =
[ 2,       5]
[ 0, 5^(1/2)]

Avoid Complex Conjugates

Use the 'real' flag to avoid complex conjugates in the result.

Create a matrix, one of the elements of which is a variable:

syms x
A = [1 2; 3 x]
A =
[ 1, 2]
[ 3, x]

Compute the QR factorization of this matrix. By default, qr assumes that x represents a complex number, and therefore, the result contains expressions with the abs function.

[Q,R] = qr(A)
Q =
[     10^(1/2)/10, -((3*x)/10 - 9/5)/(abs(x/10 - 3/5)^2...
                          + abs((3*x)/10 - 9/5)^2)^(1/2)]
[ (3*10^(1/2))/10,      (x/10 - 3/5)/(abs(x/10 - 3/5)^2...
                          + abs((3*x)/10 - 9/5)^2)^(1/2)]
 
R =
[ 10^(1/2),                           (10^(1/2)*(3*x + 2))/10]
[        0, (abs(x/10 - 3/5)^2 + abs((3*x)/10 - 9/5)^2)^(1/2)]

When you use 'real', qr assumes that all symbolic variables represent real numbers, and can return shorter results:

[Q,R] = qr(A,'real')
Q =
[     10^(1/2)/10, -((3*x)/10 - 9/5)/(x^2/10 - (6*x)/5...
                                          + 18/5)^(1/2)]
[ (3*10^(1/2))/10,      (x/10 - 3/5)/(x^2/10 - (6*x)/5...
                                          + 18/5)^(1/2)]
 
R =
[ 10^(1/2),         (10^(1/2)*(3*x + 2))/10]
[        0, (x^2/10 - (6*x)/5 + 18/5)^(1/2)]

Input Arguments

collapse all

Input matrix, specified as an m-by-n symbolic matrix.

Input, specified as a symbolic vector or matrix. The number of rows in B must be the same as the number of rows in A.

Output Arguments

collapse all

R part of the QR decomposition, returned as an m-by-n upper triangular symbolic matrix.

Q part of the QR decomposition, returned as an m-by-m unitary symbolic matrix.

Permutation information, returned as a matrix of double-precision values, such that A*P = Q*R.

Permutation information, returned as a vector of double-precision values, such that A(:,p) = Q*R.

Matrix representing solution of matrix equation A*X = B, returned as a symbolic matrix, such that C = Q'*B.

More About

collapse all

QR Factorization of Matrix

The QR factorization expresses an m-by-n matrix A as A = Q*R. Here, Q is an m-by-m unitary matrix, and R is an m-by-n upper triangular matrix. If the components of A are real numbers, then Q is an orthogonal matrix.

Tips

  • The upper triangular matrix R satisfies the following condition: R = chol(A'*A).

  • The arguments 'econ' and 0 only affect the shape of the returned matrices.

  • Calling qr for numeric matrices that are not symbolic objects (not created by sym, syms, or vpa) invokes the MATLAB® qr function.

  • If you use 'matrix' instead of 'vector', then qr returns permutation matrices, as it does by default. If you use 'matrix' and 'econ', then qr throws an error.

  • Matrix computations involving many symbolic variables can be slow. To increase the computational speed, reduce the number of symbolic variables by substituting the given values for some variables.

See Also

| | |

Introduced in R2014a