TITLE: Understanding matrices.
QUESTION [7 upvotes]: I'm trying to understand matrices.
As far as I can understand, a matrix is a way to represent data (?) or some sort of function on data (?).
However apart from the fact that they're a way to represent data (?), I really don't understand why we need them and what they're for. I'll give you an example:
I'm a game developer, and in my games I often use the well known rotation matrix. When I want to rotate a shape by some angle, I do this for every vertex:
newX = cos(angle) - sin(angle)
newY = sin(angle) + cos(angle)

This works great.
However, I don't understand why we need to represent it as a matrix (a table). The formula written above works great, what do I gain by thinking of it as a matrix?
Please explain to me what I gain by using matrices (tables) and what exactly they're used for. And please go easy on the math.

REPLY [2 votes]: One benefit that hasn't been explicitly mentioned in the other answers so far is that matrices can be composed.  Suppose you have transformations $T_1$ and $T_2$ that you want to apply to a lot of points.  If these transformations are not implemented as matrices, you'd just have to literally apply $T_1$ and then $T_2$ to all the points.  However, if the transformations are represented as matrices, you can compute a single matrix $T_3$ that corresponds to "do $T_1$ and then $T_2$" and apply that single matrix to all the points. Since $T_3$ is very easy to compute, that means you've cut your work in half.
Suppose that $T_1 = \begin{pmatrix}a&b\\c&d\end{pmatrix}$ and $T_2 = \begin{pmatrix}e&f\\g&h\end{pmatrix}$. The result of applying $T_1$ to $\begin{pmatrix}x\\y\end{pmatrix}$ is
$$\begin{pmatrix}a&b\\c&d\end{pmatrix}\begin{pmatrix}x\\y\end{pmatrix} = \begin{pmatrix}ax+by\\cx+dy\end{pmatrix}\,;$$
applying $T_2$ to that gives
$$\begin{pmatrix}e&f\\g&h\end{pmatrix}\begin{pmatrix}ax+by\\cx+dy\end{pmatrix}
    = \begin{pmatrix}e(ax+by)+f(cx+dy)\\g(ax+by)+h(cx+dy)\end{pmatrix}
    = \begin{pmatrix}(ae+cf)x + (be+df)y\\(ag+ch)x + (bg+dh)y\end{pmatrix}\,.$$
But this is exactly the same as
$$\begin{pmatrix}ae+cf & be+df\\ ag+ch & bg+dh\end{pmatrix}\begin{pmatrix}x\\y\end{pmatrix}\,,$$
so you can call this new matrix $T_3$, compute it once (now you can see how cheap it is) and then apply it to all your points. We write $T_3=T_2T_1$; note that this isn't necessarily the same as $T_1T_2$, which would mean "Do $T_2$ and then $T_1$." The same principles apply to higher-dimensional matrices.