In linear algebra, the QR decomposition of a matrix A is a factorization expressing A as
 A = QR
where Q is an orthogonal matrix (QQ^{T} = I), and R is an upper triangular matrix.
The QR decomposition is often used to solve the linear least squares problem. The QR decomposition is also the basis for a particular eigenvalue algorithm, the QR algorithm.
There are several methods for actually computing the QR decomposition, such as by means of Givens rotations, Householder transformations, or the GramSchmidt decomposition. Each has a number of advantages and disadvantages. (The matrices Q and R are not uniquely determined, so different methods may produce different decompositions.)
Computing QR by means of GramSchmidt
Recall the GramSchmidt method, with the vectors to be considered in the process as columns of the matrix . Then

Naturally then, we rearrange the equations so the a_{i}s are the subject, to get the following

Each of these projections of the vectors onto one of these e_{j} are merely the inner product of the two, since the vectors are normed.
Now these equations can be written in matrix form, viz.,
But the product of each row and column of the matrices above give us a respective column of A that we started with, and together, they give us the matrix A, so we have factorized A into an orthogonal matrix Q (the matrix of e_{k}s), via Gram Schmidt, and the obvious upper triangular matrix as a remainder R.
Alternatively, can be calculated as follows:
Recall that Then, we have
Note that and QQ^{T} = I, so Q^{T} = Q ^{ 1}.
Example
Consider the decomposition of
Recall the orthogonal matrix Q such that
Then, we can calculate Q by means of GramSchmidt as follows:
Thus, we have
Considering numerical errors of finite precision operation in MATLAB, we have that
A Householder reflection (or Householder transformation) is a transformation that takes a vector and reflects it about some plane. We can use this property to calculate the QR factorization of a matrix.
Q can be used to reflect a vector in such a way that all coordinates but one disappear. Let x be an arbitrary mdimensional column vector of length α (for numerical reasons α should get the same sign as the first coordinate of x).
Then, where e_{1} is the vector (1,0,...,0)^{T}, and   the euclidean norm, set
 .
Q is a Householder matrix and
 .
This can be used to gradually transform an mbyn matrix A to upper triangular form. First, we multiply A with the Householder matrix Q_{1} we obtain when we choose the first matrix column for x. This results in a matrix QA with zeros in the left column (except for the first line).
This can be repeated for A' resulting in a Housholder matrix Q'_{2}. Note that Q'_{2} is smaller than Q_{1}. Since we want it really to operate on Q_{1}A instead of A' we need to expand it to the upper left, filling in a 1, or in general:
After t iterations of this process, t = min(m  1,n),
is a upper triangular matrix. So, with
A = QR is a QR decomposition of A.
This method has greater numerical stability than using the GramSchmidt method above. .
Example
Let us calculate the decomposition of
We need to find a reflection that takes the vector a_{1} = (12,6,  4)^{T} to .
Now, u = (  2,6,  4)^{T} and , and then
Observe now:
So we already have almost a triangular matrix. We only need to zero the (3, 2) entry.
Take the (1, 1) minor, and then apply the process again to
By the same method as above, we obtain the matrix of the Householder transformation to be,
after performing a direct sum with 1 to make sure the next step in the process works properly.
Now, we find
The matrix Q is orthogonal and R is upper triangular, so A = QR is the required QRdecomposition.