# qr

QR decomposition of symbolic matrix

## Description

example

R = qr(A) returns the R factor of the QR decomposition A = Q*R. Here, A is an m-by-n symbolic matrix, R is an m-by-n upper triangular symbolic matrix, and Q is an m-by-m unitary symbolic 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) also returns a permutation matrix P such that A*P = Q*R. If all elements of A can be approximated by floating-point numbers, then this syntax chooses the permutation matrix P so that abs(diag(R)) is decreasing. Otherwise, it returns P = eye(n).

example

[___] = qr(A,"econ") returns the economy-size decomposition using any of the previous output argument combinations. The sizes of the outputs depend on the size of m-by-n matrix A:

• If m > n, then qr computes only the first n columns of Q and the first n rows of R.

• If mn, then the economy-size decomposition is the same as the QR decomposition without the "econ" option.

By default, when you use "econ", qr returns the permutation information as a matrix P.

example

[Q,R,P] = qr(___,outputForm) specifies whether to return the permutation information P as a matrix or a vector. For example, if outputForm is "vector", then A(:,P) = Q*R. The default value of outputForm is "matrix" such that A*P = Q*R.

[___] = qr(A,0) is equivalent to qr(A,"econ","vector"). This syntax is not recommended. Use the "econ" option instead.

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) also returns a permutation matrix P such that A*P = Q*R. If all elements of A can be approximated by floating-point numbers, then this syntax chooses the permutation matrix P so that abs(diag(R)) is decreasing. Otherwise, it returns P = eye(n).

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

[___] = qr(A,B,"econ") returns the economy-size decomposition using any of the previous output argument combinations. The sizes of the outputs depend on the size of m-by-n matrix A:

• If m > n, then qr computes only the first n rows of C and R.

• If mn, then the economy-size decomposition is the same as the QR decomposition without the "econ" option.

By default, when you use "econ", qr returns the permutation information as a matrix P.

example

[C,R,P] = qr(___,outputForm) specifies whether to return the permutation information P as a matrix or a vector. For example, if outputForm is "vector", then the solution of the matrix equation A*X = B is X(P,:) = R\C. The default value of outputForm is "matrix" such that the solution to A*X = B is X = P*(R\C).

[___] = qr(A,B,0) is equivalent to qr(A,B,"econ","vector"). This syntax is not recommended. Use the "econ" option instead.

example

___ = qr(___,"real") assumes that the input arguments and intermediate results are real, and therefore, suppresses calls to abs and conj. When you use this option, qr assumes that all symbolic variables represent real numbers. All numeric arguments should be real numbers for this syntax.

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

## Examples

collapse all

Compute the upper triangular R factor 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 =

$\left(\begin{array}{cccc}\frac{3}{2}& 1& 0& 0\\ 1& \frac{1}{2}& 1& 0\\ 0& 1& \frac{1}{2}& 1\\ 0& 0& 1& \frac{3}{2}\end{array}\right)$

Return the R factor of the QR decomposition without returning the Q factor.

R = qr(A)
R =

$\left(\begin{array}{cccc}\frac{\sqrt{13}}{2}& \frac{4 \sqrt{13}}{13}& \frac{2 \sqrt{13}}{13}& 0\\ 0& \frac{\sqrt{13} \sqrt{53}}{26}& \frac{10 \sqrt{13} \sqrt{53}}{689}& \frac{2 \sqrt{13} \sqrt{53}}{53}\\ 0& 0& \frac{\sqrt{53} \sqrt{381}}{106}& \frac{172 \sqrt{53} \sqrt{381}}{20193}\\ 0& 0& 0& \frac{35 \sqrt{381}}{762}\end{array}\right)$

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

Create the 3-by-3 Pascal matrix.

A = sym(pascal(3))
A =

$\left(\begin{array}{ccc}1& 1& 1\\ 1& 2& 3\\ 1& 3& 6\end{array}\right)$

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

[Q,R] = qr(A)
Q =

$\left(\begin{array}{ccc}\frac{\sqrt{3}}{3}& -\frac{\sqrt{2}}{2}& \frac{\sqrt{6}}{6}\\ \frac{\sqrt{3}}{3}& 0& -\frac{\sqrt{6}}{3}\\ \frac{\sqrt{3}}{3}& \frac{\sqrt{2}}{2}& \frac{\sqrt{6}}{6}\end{array}\right)$

R =

$\left(\begin{array}{ccc}\sqrt{3}& 2 \sqrt{3}& \frac{10 \sqrt{3}}{3}\\ 0& \sqrt{2}& \frac{5 \sqrt{2}}{2}\\ 0& 0& \frac{\sqrt{6}}{6}\end{array}\right)$

Verify that A = Q*R by using isAlways.

TF = isAlways(A == Q*R)
TF = 3x3 logical array

1   1   1
1   1   1
1   1   1

Use permutations to improve the numerical stability of the QR decomposition 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 with floating-point numbers.

previoussetting = digits(10);
A = vpa(hilb(3))
A =

$\left(\begin{array}{ccc}1.0& 0.5& 0.3333333333\\ 0.5& 0.3333333333& 0.25\\ 0.3333333333& 0.25& 0.2\end{array}\right)$

First, compute the QR decomposition of A without permutations.

[Q,R] = qr(A)
Q =

$\left(\begin{array}{ccc}0.8571428571& -0.5016049166& 0.1170411472\\ 0.4285714286& 0.5684855721& -0.7022468832\\ 0.2857142857& 0.6520863915& 0.7022468832\end{array}\right)$

R =

$\left(\begin{array}{ccc}1.166666667& 0.6428571429& 0.45\\ 0& 0.1017143303& 0.1053370325\\ 0& 0& 0.003901371573\end{array}\right)$

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

E = A - Q*R
E =

$\left(\begin{array}{ccc}-3.469446952e-18& -6.938893904e-18& -4.33680869e-18\\ 0& -1.734723476e-18& -8.67361738e-19\\ 0& -8.67361738e-19& -4.33680869e-19\end{array}\right)$

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 =

$\left(\begin{array}{ccc}0.8571428571& -0.4969293466& -0.1355261854\\ 0.4285714286& 0.5421047417& 0.7228063223\\ 0.2857142857& 0.6776309272& -0.6776309272\end{array}\right)$

R =

$\left(\begin{array}{ccc}1.166666667& 0.45& 0.6428571429\\ 0& 0.1054092553& 0.1016446391\\ 0& 0& 0.003764616262\end{array}\right)$

P = 3×3

1     0     0
0     0     1
0     1     0

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

E = A*P - Q*R
E =

$\left(\begin{array}{ccc}-3.469446952e-18& -4.33680869e-18& -6.938893904e-18\\ 0& -8.67361738e-19& -1.734723476e-18\\ 0& -4.33680869e-19& -1.734723476e-18\end{array}\right)$

Now, return the permutation information as a vector by using the "vector" argument.

[Q,R,p] = qr(A,"vector")
Q =

$\left(\begin{array}{ccc}0.8571428571& -0.4969293466& -0.1355261854\\ 0.4285714286& 0.5421047417& 0.7228063223\\ 0.2857142857& 0.6776309272& -0.6776309272\end{array}\right)$

R =

$\left(\begin{array}{ccc}1.166666667& 0.45& 0.6428571429\\ 0& 0.1054092553& 0.1016446391\\ 0& 0& 0.003764616262\end{array}\right)$

p = 1×3

1     3     2

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

E = A(:,p) - Q*R
E =

$\left(\begin{array}{ccc}-3.469446952e-18& -4.33680869e-18& -6.938893904e-18\\ 0& -8.67361738e-19& -1.734723476e-18\\ 0& -4.33680869e-19& -1.734723476e-18\end{array}\right)$

Exact symbolic computations do not result in round-off errors.

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

$\left(\begin{array}{ccc}0& 0& 0\\ 0& 0& 0\\ 0& 0& 0\end{array}\right)$

Restore the number of significant decimal digits to its previous setting.

digits(previoussetting)

You can solve systems of equations in matrix form by using qr.

Solve the system of equations A*X = b, where A is a 5-by-5 symbolic matrix and b is a 5-by-1 symbolic vector.

A = sym(invhilb(5))
A =

$\left(\begin{array}{ccccc}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\end{array}\right)$

b = sym([1:5]')
b =

$\left(\begin{array}{c}1\\ 2\\ 3\\ 4\\ 5\end{array}\right)$

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 =

$\left(\begin{array}{c}5\\ \frac{71}{20}\\ \frac{197}{70}\\ \frac{657}{280}\\ \frac{1271}{630}\end{array}\right)$

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

tf = isAlways(A*X == b)
tf = 5x1 logical array

1
1
1
1
1

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

Solve the system of equations A*X = b, where A is a 3-by-3 symbolic matrix with variable precision and b is a 3-by-1 symbolic vector with variable precision. Find matrices C and R such that C = Q'*b and A = Q*R.

previoussetting = digits(10);
A = vpa([2 -3 -1; 1 1 -1; 0 1 -1]);
b = vpa([2; 0; -1]);
[C,R,P] = qr(A,b)
C =

$\left(\begin{array}{c}-2.110579412\\ -0.2132007164\\ 0.7071067812\end{array}\right)$

R =

$\left(\begin{array}{ccc}3.31662479& 0.3015113446& -1.507556723\\ 0& 1.705605731& -1.492405014\\ 0& 0& 0.7071067812\end{array}\right)$

P = 3×3

0     0     1
1     0     0
0     1     0

Compute the solution X.

X = P*(R\C)
X =

$\left(\begin{array}{c}1.0\\ -0.25\\ 0.75\end{array}\right)$

Alternatively, return the permutation information as a vector.

[C,R,p] = qr(A,b,"vector")
C =

$\left(\begin{array}{c}-2.110579412\\ -0.2132007164\\ 0.7071067812\end{array}\right)$

R =

$\left(\begin{array}{ccc}3.31662479& 0.3015113446& -1.507556723\\ 0& 1.705605731& -1.492405014\\ 0& 0& 0.7071067812\end{array}\right)$

p = 1×3

2     3     1

Compute the solution X with permutation vector p.

X(p,:) = R\C
X =

$\left(\begin{array}{c}1.0\\ -0.25\\ 0.75\end{array}\right)$

Restore the number of significant decimal digits to its previous setting.

digits(previoussetting)

Compute the economy-size QR decomposition by using the "econ" option.

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 =

$\left(\begin{array}{cc}1& 1\\ 1& 2\\ 1& 3\\ 1& 4\end{array}\right)$

Compute the QR decomposition for this matrix.

[Q,R] = qr(A)
Q =

R =

$\left(\begin{array}{cc}2& 5\\ 0& \sqrt{5}\\ 0& 0\\ 0& 0\end{array}\right)$

Now, compute the economy-size QR decomposition for this matrix. Because the number of rows is greater than the number of columns in A, qr computes only the first 2 columns of Q and the first 2 rows of R.

[Q,R] = qr(A,"econ")
Q =

$\left(\begin{array}{cc}\frac{1}{2}& -\frac{3 \sqrt{5}}{10}\\ \frac{1}{2}& -\frac{\sqrt{5}}{10}\\ \frac{1}{2}& \frac{\sqrt{5}}{10}\\ \frac{1}{2}& \frac{3 \sqrt{5}}{10}\end{array}\right)$

R =

$\left(\begin{array}{cc}2& 5\\ 0& \sqrt{5}\end{array}\right)$

You can avoid complex conjugates in the result of QR decomposition by using the "real" option.

Create a matrix that includes a symbolic variable as an element.

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

$\left(\begin{array}{cc}1& 2\\ 3& x\end{array}\right)$

Compute the QR decomposition 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 =

R =

$\left(\begin{array}{cc}\sqrt{10}& \frac{\sqrt{10} \left(3 x+2\right)}{10}\\ 0& \sqrt{{|\frac{x}{10}-\frac{3}{5}|}^{2}+{|\frac{3 x}{10}-\frac{9}{5}|}^{2}}\end{array}\right)$

When you use the "real" option, qr assumes that all symbolic variables represent real numbers and can return shorter results.

[Q,R] = qr(A,"real")
Q =

$\left(\begin{array}{cc}\frac{\sqrt{10}}{10}& -\frac{\frac{3 x}{10}-\frac{9}{5}}{\sqrt{\frac{{x}^{2}}{10}-\frac{6 x}{5}+\frac{18}{5}}}\\ \frac{3 \sqrt{10}}{10}& \frac{\frac{x}{10}-\frac{3}{5}}{\sqrt{\frac{{x}^{2}}{10}-\frac{6 x}{5}+\frac{18}{5}}}\end{array}\right)$

R =

$\left(\begin{array}{cc}\sqrt{10}& \frac{\sqrt{10} \left(3 x+2\right)}{10}\\ 0& \sqrt{\frac{{x}^{2}}{10}-\frac{6 x}{5}+\frac{18}{5}}\end{array}\right)$

## Input Arguments

collapse all

Symbolic input, specified as an m-by-n symbolic matrix.

Symbolic input, specified as a symbolic vector or matrix. B must have the same number of rows as A.

Shape of the permutation output, specified as "matrix" or "vector". This option controls whether the permutation output P is returned as a permutation matrix or vector. You must specify three output arguments to qr to use this option.

• If outputForm is "matrix", then P is a permutation matrix that satisfies A*P = Q*R.

• If outputForm is "vector", then P is a permutation vector that satisfies A(:,P) = Q*R.

## Output Arguments

collapse all

R factor of the QR decomposition, returned as an upper triangular symbolic matrix that satisfies A = Q*R for an m-by-n matrix A.

Q factor of the QR decomposition, returned as a unitary symbolic matrix that satisfies A = Q*R for an m-by-n matrix A.

• For full decomposition, qr(A) returns Q as an m-by-m unitary matrix.

• For rectangular matrix A with m > n, the economy-size decomposition qr(A,"econ") computes only the first n columns of Q and first n rows of R.

Permutation information, returned as a matrix or vector of double-precision values. The shape of P depends on the value of outputForm.

Linear system factor, returned as a symbolic matrix representing the solution of the matrix equation A*X = B, such that C = Q'*B and X = R\C. If you specify the permutation output P, then the solution is either X = P*(R\C) when outputForm is "matrix" or X(P,:) = R\C when outputForm is "vector".

collapse all

### QR Decomposition of Matrix

The QR decomposition, also known as the QR factorization, expresses an m-by-n matrix A as A = Q*R. For the full decomposition, 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 condition R = chol(A'*A).

• The arguments "econ" and 0 affect only 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.

• 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.

## Version History

Introduced in R2014a

expand all