Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Recipe

Factor a matrix as A=QR

Use when: you have a matrix A with linearly independent columns and need Q (orthonormal columns) and R (upper triangular) with A=QR

Goal: both Q and R, built together in one pass of Gram-Schmidt, with no separate computation for R afterward

Taught in QR factorization

  1. 1

    Normalize \mathbf a_1: \mathbf q_1=\mathbf a_1/\lVert\mathbf a_1\rVert. Record r_{11}=\lVert\mathbf a_1\rVert — the first diagonal entry of R, not a separate computation.

    Example

    For A=\begin{bmatrix}3&1\\4&7\end{bmatrix}, columns \mathbf a_1=(3,4), \mathbf a_2=(1,7): \lVert\mathbf a_1\rVert=\sqrt{3^2+4^2}=5, so \mathbf q_1=(0.6,0.8) and r_{11}=5.

    Why: nothing has been subtracted from \mathbf a_1 yet, so \mathbf q_1 is forced to point exactly where \mathbf a_1 already points — only its length changes.

  2. 2

    For column \mathbf a_k, compute r_{ik}=\mathbf q_i\cdot\mathbf a_k for every earlier \mathbf q_i (i<k) — the same projection coefficients ordinary Gram-Schmidt needs, just written down instead of thrown away.

    Example

    r_{12}=\mathbf q_1\cdot\mathbf a_2=0.6(1)+0.8(7)=0.6+5.6=6.2.

    Why: r_{ik} is defined to be exactly this projection coefficient — Gram-Schmidt was already computing it to know how much of \mathbf q_i to strip out of \mathbf a_k.

  3. 3

    \mathbf w_k=\mathbf a_k-\sum_{i<k}r_{ik}\mathbf q_i.

    Example

    \mathbf w_2=\mathbf a_2-r_{12}\mathbf q_1=(1,7)-6.2(0.6,0.8)=(1,7)-(3.72,4.96)=(-2.72,2.04).

    Why: removing every already-built direction leaves only the genuinely new part of \mathbf a_k — this is ordinary Gram-Schmidt, unchanged.

  4. 4

    r_{kk}=\lVert\mathbf w_k\rVert; \mathbf q_k=\mathbf w_k/r_{kk}. Repeat Steps 2–4 for every remaining column, in order, before moving to the last step.

    Example

    r_{22}=\lVert\mathbf w_2\rVert=\sqrt{2.72^2+2.04^2}=\sqrt{11.56}=3.4, so \mathbf q_2=(-2.72,2.04)/3.4=(-0.8,0.6).

    Why: this is the same reasoning as Step 1's diagonal entry — R's diagonal always records how long the leftover piece was, right before it gets rescaled to length 1.

  5. 5

    Stack \mathbf q_1,\dots,\mathbf q_n as the columns of Q; place every r_{ik} found above into R (everything below the diagonal is exactly 0). Then confirm QR reproduces A, column by column.

    Example

    Q=\begin{bmatrix}0.6&-0.8\\0.8&0.6\end{bmatrix},\qquad R=\begin{bmatrix}5&6.2\\0&3.4\end{bmatrix}

    Check column 2: r_{12}\mathbf q_1+r_{22}\mathbf q_2=6.2(0.6,0.8)+3.4(-0.8,0.6)=(3.72,4.96)+(-2.72,2.04)=(1,7)=\mathbf a_2. ✓

    Why: this catches an arithmetic slip in a single projection or normalization before treating Q and R as final — and it's literally the content of the theorem this section proves, so checking it costs nothing extra.

Common mistakes

  • Subtracting only the most recent \mathbf q instead of every earlier one when A has three or more columns.
  • Throwing away the projection coefficients from Step 2 and recomputing R afterward as Q^{\mathsf T}A — correct, but it defeats the entire point of this section, that R comes for free.
  • Writing a nonzero entry below R's diagonal — R is upper triangular by construction, never just "mostly triangular."
  • Skipping the final reconstruction check and trusting Q, R without confirming QR=A.