# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English version of the page.

To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

# qr

Orthogonal-triangular decomposition

## Syntax

```[Q,R] = qr(A) [Q,R] = qr(A,0) [Q,R,E] = qr(A) [Q,R,E] = qr(A,'matrix') [Q,R,e] = qr(A,'vector') [Q,R,e] = qr(A,0) X = qr(A) X = qr(A,0) R = qr(A) R = qr(A,0) [C,R] = qr(A,B) [C,R,E] = qr(A,B) [C,R,E] = qr(A,B,'matrix') [C,R,e] = qr(A,B,'vector') [C,R] = qr(A,B,0) [C,R,e] = qr(A,B,0) ```

## Description

`[Q,R] = qr(A)`, where `A` is `m`-by-`n`, produces an `m`-by-`n` upper triangular matrix `R` and an `m`-by-`m` unitary matrix `Q` so that `A = Q*R`.

`[Q,R] = qr(A,0)` produces the economy-size decomposition. If `m > n`, only the first `n` columns of `Q` and the first `n` rows of `R` are computed. If `m<=n`, this is the same as ```[Q,R] = qr(A)```.

If A is full:

`[Q,R,E] = qr(A)` or ```[Q,R,E] = qr(A,'matrix')``` produces unitary `Q`, upper triangular `R` and a permutation matrix `E` so that `A*E = Q*R`. The column permutation `E` is chosen so that `abs(diag(R))` is decreasing.

`[Q,R,e] = qr(A,'vector')` returns the permutation information as a vector instead of a matrix. That is, `e` is a row vector such that `A(:,e) = Q*R`.

`[Q,R,e] = qr(A,0)` produces an economy-size decomposition in which `e` is a permutation vector, so that `A(:,e) = Q*R`.

`X = qr(A)` and `X = qr(A,0)` return a matrix `X` such that `triu(X)` is the upper triangular factor `R`.

If A is sparse:

`R = qr(A)` computes a `Q`-less `QR` decomposition and returns the upper triangular factor `R`. Note that `R = chol(A'*A)`. Since `Q` is often nearly full, this is preferred to `[Q,R] = QR(A)`.

`R = qr(A,0)` produces economy-size `R`. If `m>n`, `R` has only `n` rows. If `m<=n`, this is the same as `R = qr(A)`.

`[Q,R,E] = qr(A)` or `[Q,R,E] = qr(A,'matrix')` produces unitary `Q`, upper triangular `R` and a permutation matrix `E` so that `A*E = Q*R`. The column permutation `E` is chosen to reduce fill-in in R.

`[Q,R,e] = qr(A,'vector')` returns the permutation information as a vector instead of a matrix. That is, `e` is a row vector such that `A(:,e) = Q*R`.

`[Q,R,e] = qr(A,0)` produces an economy-size decomposition in which `e` is a permutation vector, so that `A(:,e) = Q*R`.

`[C,R] = qr(A,B)`, where `B` has as many rows as `A`, returns `C = Q'*B`. The least-squares solution to `A*X = B` is ```X = R\C```.

`[C,R,E] = qr(A,B)` or ```[C,R,E] = qr(A,B,'matrix')```, also returns a fill-reducing ordering. The least-squares solution to `A*X = B` is ```X = E*(R\C)```.

`[C,R,e] = qr(A,B,'vector')` returns the permutation information as a vector instead of a matrix. That is, the least-squares solution to `A*X = B` is ```X(e,:) = R\C```.

`[C,R] = qr(A,B,0)` produces economy-size results. If `m>n`, `C` and `R` have only `n` rows. If `m<=n`, this is the same as `[C,R] = qr(A,B)`.

`[C,R,e] = qr(A,B,0)` additionally produces a fill-reducing permutation vector `e`. In this case, the least-squares solution to `A*X = B` is ```X(e,:) = R\C```.

## Examples

Find the least squares approximate solution to ```A*x = b``` with the `Q`-less `QR` decomposition and one step of iterative refinement:

```if issparse(A), R = qr(A); else R = triu(qr(A)); end x = R\(R'\(A'*b)); r = b - A*x; err = R\(R'\(A'*r)); x = x + err;```