# At a Glance - Multiplying Matrices

## Multiplying Matrices: Further Down the Rabbit Hole

So far, we've been dealing with operations that were reasonably simple: adding and subtracting matrices is limited to same-sized matrices, and scalar multiplication just runs the one number through the whole matrix.

But get this: actual matrices can also be multiplied against each other. This is a little more complicated, but basically you just need to remember the most important thing: size matters—at least for multiplying matrices.

The sizes of the matrices involved are the most important factor here, so be careful: for matrices to be multiplied together, the number of *columns* in the first matrix must be the same as the number of *rows* in the second. And it'll make your life way, way easier if you take a sec to refresh your memory on the difference between a row and a column. Remember, **rows** are *horizontal* (left to right), and **columns** are *vertical* (top to bottom).

### Sample Problem

Say we've got matrix *A* and matrix *B*:

Both the size of the matrices and the order we multiply them in matters.

*AB* is something we can't do, because there are two columns in *A* and three rows in *B*. Game over, man.

*BA* we can do, because *B* has two columns and *A* has two rows. Perfect. The next thing to remember is that *the product matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix*. Yeah, it's so important we italicized the whole blasted thing.

Onward to *BA*. Here's how that looks:

We know the product will have three rows like *B* does, and two columns like *A* does:

We already know the first matrix here is *B*, and the second one is *A*. Let's call our product *P*. In that case, the entries in the matrices could be labeled like so:

You're going to want to go across and down to work this out:

*P*_{11} = (*B*_{11})(*A*_{11}) + (*B*_{12})(*A*_{21})

We multiply and add the entries of the first row of the first matrix with those of the first column of the second matrix. That's why the sizes have to match, so nothing is left over. Then, for the next entry in our product:

*P*_{12} = (*B*_{11})(*A*_{12}) + (*B*_{12})(*A*_{22})

Now we move on to the next part. You multiply and add the entries of the second row of the first matrix with those of the first column of the second matrix. Buckle up.

*P*_{21} = (*B*_{21})(*A*_{11}) + (*B*_{22})(*A*_{21})

Then you smash together the second row of the first matrix with the second column of the second matrix. That gives you the second row of your product.

*P*_{22} = (*B*_{21})(*A*_{12}) + (*B*_{22})(*A*_{22})

Almost there. Just one more row, and you can probably guess how it goes:

*P*_{31} = (*B*_{31})(*A*_{11}) + (*B*_{32})(*A*_{21})

*P*_{32} = (*B*_{31})(*A*_{12}) + (*B*_{32})(*A*_{22})

Plugging all that junk into the big picture, we get this:

Here's another way of looking at it. We know from the rules that *P* is going to have three rows like *B* and two columns like *A*. Try and visualize the three of them together like this:

Looking at where the intersections are, we can see which row of *B* and which column of *A* collide to come up with one of *P*'s entries. Below, we can see that X marks the spot where row one of *B* and column one of *A* crash:

We know those four numbers need to crunch into one. How do they do that? Well, we read rows left to right, like a book. We read columns top to bottom. Therefore, we multiply the first number from *B*'s first row with the first number from *A*'s first column:

(1)(2) = 2

Then we multiply the second number from *B*'s first row with the second number from *A*'s first column:

(3)(3) = 9

And we add those together:

(1)(2) + (3)(3) = 2 + 9 = 11

Next we check out the collision between row one of *B* and column two of *A*:

Yup; X marks the spot, and we do the same thing. We read rows left to right, like a book. And we read columns top to bottom. We multiply the first number from *B*'s first row with the first number from *A*'s second column: (1)(1) = 1. Then we multiply the second number from *B*'s first row with the second number from *A*'s second column: (3)(2) = 6. Add those together to get our entry for X:

(1)(1) + (3)(2) = 1 + 6 = 7

We know, we know. We're like a broken record, but X marks the spot again, and we do the same thing. We read the row left to right, and we read the column top to bottom. This time we're doing the second row in the product, so we multiply the first number from *B*'s second row with the first number from *A*'s first column: (2)(2) = 4. Then multiply the second number from *B*'s second row with the second number from *A*'s first column: (1)(3) = 3. Now add 'em:

(2)(2) + (1)(3) = 4 + 3 = 7

We're really getting somewhere now. Halfway there.

Now it looks like this, and we can see what's next:

X marks the spot, friend, and we do the same thing. The row goes left to right, the column top to bottom. This time we're still doing the second row in the product, but we're moving on to the second spot in it. We multiply the first number from *B*'s second row with the first number from *A*'s second column: (2)(1) = 2. Then we multiply the second number from *B*'s second row with the second number from *A*'s second column: (1)(2) = 2. Add those dudes:

(2)(1) + (1)(2) = 2 + 2 = 4

And we do it all over again, this time with the third row in *B*, once for each entry in the first column of *A*.

(4)(2) + (2)(3) = 8 + 6 = 14

And finally, we smash together the third row of *B* with the second column of *A*:

(4)(1) + (2)(2) = 4 + 4 = 8

Woohoo! We did it!

We kept our heads, and it only gets easier so long as we remember the most important rules:

- For matrices to be multiplied together, the number of columns in the first matrix must be the same as the number of rows in the second.
- The product matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix.

Hey, remember when we said that even when we have two matrices of the same size, the order matters? You can multiply in either order, sure, but the answer won't be the same. Watch:

### Sample Problem

We can do it in this order first:

(2)(1) + (4)(4) = 2 + 16 = 18

(2)(0) + (4)(2) = 0 + 8 = 8

(3)(1) + (3)(4) = 3 + 12 = 15

(3)(0) + (3)(2) = 0 + 6 = 6

So this is our product:

And if we turn the tables?

Let's arrange that so it's easier to see:

(1)(2) + (0)(3) = 2 + 0 = 2

(1)(4) + (0)(3) = 4 + 0 = 4

(4)(2) + (2)(3) = 8 + 6 = 14

(4)(4) + (2)(3) = 16 + 6 = 22

This is our product:

Totally different. Weird, right?

Hold the presses, though. Remember the identity matrix and zero matrix? Theeeey're baaaack...

### Thinking in Binary

The one time a number can be multiplied and stay the same is when it's multiplied by the number 1.

3 × 1 = 3

100 × 1 = 100

457,820 × 1 = 457,820

In Matrix Land, the identity matrix is the matrix version of 1. Identity matrices are all 0s except for a diagonal line of 1s from the top left to bottom right corners. They can be any dimensions, so long as they're square:

Here's the big deal: whenever a matrix is multiplied by an identity matrix, the product is the same as the non-identity matrix. And we can prove it.

### Sample Problem

We proceed just as we did in multiplying before. We'll call the identity matrix *I*, the product *P*, and the other matrix we'll call *T* for Ted. We just like the name, okay? Try and visualize the three of them together like this:

So we combine *T*'s first row with *I*'s first column to find X:

(2)(1) + (3)(0) + (2)(0) = 2 + 0 + 0 = 2

We continue on in that pattern and get a product that's exactly the same as *T*, because multiplying a matrix by *I* is like multiplying a number by the number 1.

Rule: *Whenever we multiply a matrix by an identity matrix, the answer is always the same as the non-identity matrix. *Yep, we brought the italics back for this uber-important rule.

And the zero matrix? Well, we remember that any number multiplied by zero is just zero. A zero matrix functions in matrix multiplication the very same way. These are all zero matrices:

Rule: *Whenever we multiply a matrix by a zero matrix, the answer is always a zero matrix that has the same number of rows as the first matrix and the same number of columns as the second matrix.*

### Sample Problem

We know they can be multiplied together because the number of columns in the first matrix (3) matches the number of rows in the second one (also 3). We also know that the product matrix will be a 3 × 3 matrix because the product matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix.

Boom:

Yep, it's that simple. Anything times zero is zero, even in Matrix Land.