# Project a vector onto subspace spanned by columns of a matrix

From this question we know that if $$x\in\mathbb{R}^{n\times1}$$ is a vector, then the (normalized) outer product matrix $$\frac{x x^\top}{||x||^2}\, \in \mathbb{R}^{n\times n}$$ can operate on another vector $$y\in\mathbb{R}^{n\times 1}$$ and projects orthogonally onto the line spanned by $$x$$.

How does this generalize to a basis of vectors?

Suppose $$X\in\mathbb{R}^{n\times m}$$ is a matrix and let $$[x_1 \, \cdots x_m]$$ be its columns. I would like to find a matrix $$A\in\mathbb{R}^{n\times n}$$ such that it projects a vector $$y\in\mathbb{R}^{n\times 1}$$ onto the space spanned by the columns on $$X$$.

I have chosen to rewrite my answer since my recollection of the formula was not quite satisfactionary. The formula I presented actually holds in general. If $$A$$ is a matrix, the matrix

$$P = A (A^\top A)^{-1} A^\top$$

is always the projection onto the column space of $$A$$. In the case where $$A$$ is orthogonal, this reduces to $$AA^\top$$. You can find a simple derivation under "Intuition" in this Wikipedia-article: https://en.wikipedia.org/wiki/Projection_matrix.

• Thank you! How did you arrive at the expression for $P$? Also, in my very specific case I am quite lucky cause the columns are already normalized, so that works perfectly! Is there a way to perform this projection more efficiently in practice? Jun 22, 2021 at 9:36
• Perhaps using some matrix decomposition Jun 22, 2021 at 9:39
• I have edited the answer. Jun 22, 2021 at 10:06

let consider a generic vector $${v}$$. one can decompose this vector in the tangential and the orthogonal vector components as follows

$$v=(v\cdot \hat{n})\hat{n}+(v\cdot\hat{t})\hat{t}$$

moving the normal component to the LHS it has

$$v\cdot(I-\hat{n}\hat{n})=(v\cdot\hat{t})\hat{t}$$

where $$I$$ is the identity matrix. Therefore one can define the rejection and the projection operators

$$P=I-\hat{n}\hat{n}$$

$$R=\hat{n}\hat{n}$$