QR decomposition of symbolic matrix
collapse all in page
Syntax
R = qr(A)
[Q,R] =qr(A)
[Q,R,P]= qr(A)
[___] = qr(A,"econ")
[Q,R,P]= qr(___,outputForm)
[___] = qr(A,0)
[C,R] =qr(A,B)
[C,R,P]= qr(A,B)
[___] = qr(A,B,"econ")
[C,R,P]= qr(___,outputForm)
[___] = qr(A,B,0)
___ = qr(___,"real")
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 m ≤ n, 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 firstn
rows of C and R.If m ≤ n, 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
Upper Triangular Factor of QR Decomposition
Open Live Script
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 =
Return the R
factor of the QR decomposition without returning the Q
factor.
R = qr(A)
R =
QR Decomposition of Pascal Matrix
Open Live Script
Compute the QR decomposition of the 3-by-3 Pascal matrix.
Create the 3-by-3 Pascal matrix.
A = sym(pascal(3))
Find the Q
and R
matrices representing the QR decomposition of A
.
[Q,R] = qr(A)
Q =
R =
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
Permuted QR Decomposition
Open Live Script
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 =
First, compute the QR decomposition of A
without permutations.
[Q,R] = qr(A)
Q =
R =
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 =
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 =
R =
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 =
Now, return the permutation information as a vector by using the "vector"
argument.
[Q,R,p] = qr(A,"vector")
Q =
R =
p = 1×3 1 3 2
Verify that A(:,p) = Q*R
.
E = A(:,p) - Q*R
E =
Exact symbolic computations do not result in round-off errors.
A = sym(hilb(3));[Q,R] = qr(A);E = A - Q*R
E =
Restore the number of significant decimal digits to its previous setting.
digits(previoussetting)
Solve Matrix Equation with QR Decomposition
Open Live Script
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 =
b = sym([1:5]')
b =
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 =
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
Solve Matrix Equation with Permuted QR Decomposition
Open Live Script
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 =
R =
P = 3×3 0 0 1 1 0 0 0 1 0
Compute the solution X
.
X = P*(R\C)
X =
Alternatively, return the permutation information as a vector.
[C,R,p] = qr(A,b,"vector")
C =
R =
p = 1×3 2 3 1
Compute the solution X
with permutation vector p
.
X(p,:) = R\C
X =
Restore the number of significant decimal digits to its previous setting.
digits(previoussetting)
Economy-Size QR Decomposition
Open Live Script
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 =
Compute the QR decomposition for this matrix.
[Q,R] = qr(A)
Q =
R =
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 =
R =
Avoid Complex Conjugates
Open Live Script
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 xA = [1 2; 3 x]
A =
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 =
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 =
R =
Input Arguments
collapse all
A
— Symbolic input
m-by-n symbolic matrix
Symbolic input, specified as an m-by-n symbolic matrix.
B
— Symbolic input
symbolic vector | symbolic matrix
Symbolic input, specified as a symbolic vector or matrix. B
must have the same number of rows as A.
outputForm
— Shape of permutation output
"matrix"
(default) | "vector"
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"
, thenP
is a permutation matrix that satisfiesA*P = Q*R
.If
outputForm
is"vector"
, thenP
is a permutation vector that satisfiesA(:,P) = Q*R
.
Output Arguments
collapse all
R
— R factor of QR decomposition
upper triangular symbolic matrix
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
— Q factor of QR decomposition
unitary symbolic matrix
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)
returnsQ
as an m-by-m unitary matrix.For rectangular matrix
A
with m > n, the economy-size decompositionqr(A,"econ")
computes only the first n columns ofQ
and first n rows ofR
.
P
— Permutation information
matrix | vector
Permutation information, returned as a matrix or vector of double-precision values. The shape of P
depends on the value of outputForm.
C
— Linear system factor
symbolic matrix
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"
.
More About
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"
and0
affect only the shape of the returned matrices.Calling
qr
for numeric matricesthat are not symbolic objects (not created bysym
,syms
,orvpa
) 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
R2023a: Economy-size decomposition returns permutation information as matrix
The syntaxes [Q,R,P] = qr(A,"econ")
and [C,R,P] = qr(A,B,"econ")
for economy-size decomposition of symbolic inputs now return the permutation information as a matrix P
. In previous releases, these syntaxes returned the permutation information as a vector P
.
Starting in R2023a, the syntaxes
qr(__,"econ")
andqr(__,"econ","matrix")
are equivalent. They both return the permutation information as a matrixP
. Furthermore, the syntaxesqr(__,0)
andqr(__,"econ","vector")
are equivalent. They both return the permutation information as a vectorP
.Before R2023a, the syntaxes
qr(__,"econ")
,qr(__,0)
, andqr(__,"econ","vector")
were equivalent. They all returned the permutation information as a vectorP
. The syntaxqr(__,"econ","matrix")
previously returned an error.
See Also
chol | eig | lu | svd
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office