### CHECK OUT SHMOOP'S FREE STUDY TOOLS:

### CHECK OUT SHMOOP'S FREE STUDY TOOLS:

# Multiplying Matrices

## Multiplying Matrices: Further Down the Rabbit Hole

Thus 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 matrix. 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.*

### Sample Problem

So, if we have 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 has and the same number of columns that the second matrix has*.

Looking back to our example, we cannot multiply AB, but BA we can do. Like we said, we pity the fool, just like B.A. Baracus. 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})

So far our quest for P looks like this:

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; then you do it with the second column of the second matrix. That gives you the second row of your product. This is way less confusing using pictures:

P_{21} = (B_{21})(A_{11}) + (B_{22})(A_{21})

Next, we complete row two of the product:

P_{22} = (B_{21})(A_{12}) + (B_{22})(A_{22})

And plugging that into the big picture:

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

P_{31} = (B_{31})(A_{11}) + (B_{32})(A_{21})

and

P_{32} = (B_{31})(A_{12}) + (B_{32})(A_{22})

make up the third row in our product. In place in the whole it looks like this:

Another way of looking at it is this. 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, 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: 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, we multiply the second number from B's first row with the second number from A's second column (3)(2) = 6, we add those together: 1 + 6 = 7.

We know, we know. We're like a broken record, but -X marks the spot, and we do the same thing. We read the row left to right, we read the column top to bottom. This time we are 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, we multiply the second number from B's second row with the second number from A's first column (1)(3) = 3, we add those together: 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 are still doing the second row in the product, we are just 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, we multiply the second number from B's second row with the second number from A's second column (1)(2) = 2, we add those together: 2 + 2 = 4, to get this:

And we do it all over again, this time with the third row in B, once for each column in A. (4)(2) = 8, (2)(3) = 6, and 8 + 6 = 14:

And finally, (4)(1) = 4, (2)(2) = 4, and 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; *and* the product matrix will have the same number of rows as the first matrix has and the same number of columns that the second matrix has.*

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:

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...

The one time a number can be multiplied and stay the same is when it is multiplied by the number one. In matrix land, the identity matrix is the one. Identity matrices are all zeros but for a diagonal line of ones from the top left to bottom right corners. They can be any dimensions so long as they are square:

*Whenever a matrix is multiplied by an identity matrix, the product is the same as the non-identity matrix*:

We proceed just as we did in multiplying before. Try and visualize the three of them together like this:

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? Looking at where the intersections are, we can see which row of T and which column of I collide to come up with one of P's entries. Below, we can see that X marks the spot where row one of T and column one of I crash:

So we multiply the first number from T's first row with the first number from I's first column (2)(1) = 2, we multiply the second number from T's first row with the second number from I's first column (3)(0) = 0, we multiply the third number from T's first row with the third number from I's first column (2)(0) = 0 and we add those together to get 2.

Next we check out the collision between row one of T and column two of I:

Yup. X marks the spot, and we do the same thing. So we multiply the first number from T's first row with the first number from I's second column (2)(0) = 0, we multiply the second number from T's first row with the second number from I's second column (3)(1) = 3, we multiply the third number from T's first row with the third number from I's second column (2)(0) = 0, and we add those together: 0 + 3 + 0 = 3.

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 there's an operation that can be multiplied together and one of the matrices is an identity matrix, the answer is always the same as the non-identity matrix.*

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:

*Whenever there's an operation that is able to be multiplied together and one of the matrices is a zero matrix, the answer is always a zero matrix that has the same number of rows as the first matrix has and the same number of columns that the second matrix has.*

Take this example:

We know they can be multiplied together because *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.*That's true here; there are three columns in the first matrix and three rows in the second matrix. We also know that the product matrix will also be a 3 × 3 matrix because*the product matrix will have the same number of rows as the first matrix has and the same number of columns that the second matrix has.*

Applying the zero matrix rule we see that w*henever there's an operation that is able to be multiplied together and one of the matrices is a zero matrix, the answer is always a zero matrix that has the same number of rows as the first matrix has and the same number of columns that the second matrix has.*

Therefore:

### Sample Problem

Find the product:

Answer:

We got this one. The relevant 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; *and* the product matrix will have the same number of rows as the first matrix has and the same number of columns that the second matrix has.* This means that we can do this calculation and the product will be a 3 × 3.

Let's look at this the easy way:

That's right. We look at the intersection and we can see the collision. X marks the spot where row one of the first matrix and column one of the second matrix crash:

We know those four numbers need to crunch into one, just like they crunch our car into a little rectangle at the wrecking yard after we get in a crash. We've been distracted lately by matrices, right? We look at the first matrix's first row left to right. We read the second matrix's first column top to bottom. We multiply the first number from the first matrix's first row with the first number from the second matrix's first column (5)(3) = 15, we multiply the second number from the first matrix's first row with the second number from the second matrix's first column (2)(5) = 10, and we add those together: 15 + 10 = 25.

Next we check out the next collision like the good rubberneckers that we are:

X marks the spot, and we do the same thing. We read rows left to right and we read columns top to bottom. We multiply the first number from the first matrix's first row with the first number from the second matrix's second column (5)(6) = 30, we multiply the second number from the first matrix's first row with the second number from the second matrix's second column (2)(2) = 4, we add those together:

30 + 4 = 34.

Now we just do the last one in that top row of the product. X marks our target spot and we know that we multiply the first number from the first matrix's first row with the first number from the second matrix's third column (5)(4) = 20, we multiply the second number from the first matrix's first row with the second number from the second matrix's third column (2)(2) = 4, we add those together: 20 + 4 = 24.

We plug that in and move on to the second row of our product:

We press on through the rest of the product—X marks the spot, and we do the same thing. This time we are doing the second row in the product, so we multiply the first number from the first matrix's second row with the first number from the second matrix's first column (2)(3) = 6, we multiply the second number from the first matrix's second row with the second number from the second matrix's first column (1)(5) = 5, we add those together: 6 + 5 = 11.

Now it looks like this, and we can see that Alice's Brady Bunch spot is next:

To get our Alice X we multiply the first number from the first matrix's second row with the first number from the second matrix's second column (2)(6) = 12, we multiply the second number from the first matrix's second row with the second number from the second matrix's first column (1)(2) = 2, we add those together: 12 + 2 = 14.

Now it looks like this, and what's next—is that Jan Brady? Hmmm:

That's right, Marcia. We multiply the first number from the first matrix's second row with the first number from the second matrix's third column (2)(4) = 8, we multiply the second number from the first matrix's second row with the second number from the second matrix's third column (1)(2) = 2, we add those together: 8 + 2 = 10.

This is going swimmingly:

We multiply the first number from the first matrix's third row with the first number from the second matrix's first column (3)(3) = 9, we multiply the second number from the first matrix's third row with the second number from the second matrix's first column (4)(5) = 20, we add those together: 9 + 20 = 29.

Now what?

We multiply the first number from the first matrix's third row with the first number from the second matrix's second column (3)(6) = 18, we multiply the second number from the first matrix's third row with the second number from the second matrix's second column (4)(2) = 8, we add those together: 18 + 8 = 26.

Yes, last one. We multiply the first number from the first matrix's third row with the first number from the second matrix's third column (3)(4) = 12, we multiply the second number from the first matrix's third row with the second number from the second matrix's third column (4)(2) = 8, we add those together: 12 + 8 = 20.

Our product: