How to find the matrix representation of a linear tranformation

Let $\mathcal B = \{b_1, \dots, b_m\}$ and $\mathcal D = \{d_1, \dots, d_n\}$. Then the matrix $[T]_{\mathcal D\leftarrow \mathcal B}$ is given by $$[T]_{\mathcal D\leftarrow \mathcal B} = \begin{bmatrix}[T(b_1)]_{\mathcal D} & \cdots & [T(b_m)]_{\mathcal D}\end{bmatrix}$$ where $[T(b_i)]_{\mathcal D}$ is the column whose coordinates are those of the image of $b_i$ under $T$ represented in the basis $\mathcal D$. Why does this work?


Example. Let's say I have $\mathcal B = \{(1,1),(1,2)\}\subset \Bbb R^2$ and $\mathcal D = \{(1,0),(1,1)\}\subset \Bbb R^2$. Consider the linear operator, $P$, which projects vectors in $\Bbb R^2$ onto the line spanned by $(0,1)$. Then first we find $P(b_1)$ and $P(b_2)$.

$$P(b_1) = \frac{(1,1)\cdot(0,1)}{1^2}(0,1) = (0,1) \\ P(b_2) = \frac{(1,2)\cdot (0,1)}{1^2}(0,1) = (0,2)$$

Then we express these vectors in the basis $\mathcal D$:

$$P(b_1) = (0,1) = -(1,0) + (1,1) \\ P(b_2) = (0,2) = -2(1,0) + 2(1,1)$$

Now we can immediately construct the matrix $[P]_{\mathcal D \leftarrow \mathcal B}$:

$$[P]_{\mathcal D \leftarrow \mathcal B} = \begin{bmatrix} -1 & -2 \\ 1 & 2\end{bmatrix}$$