Main content

### Course: Algebra (all content) > Unit 20

Lesson 8: Multiplying matrices by matrices# Multiplying matrices

Sal gives an example of a multiplication of two matrices that don't have the same dimensions. Created by Sal Khan.

## Want to join the conversation?

- Why is matrix multiplication defined so that the number of rows in matrix1 have to match the number columns of matrix2 and so on?

While I don't have a deeper understanding of the subject it would seem more intuitive to require matching columns to columns and rows to rows.(40 votes)- Matrices represent linear functions between vector spaces. It is hard to explain without going into linear algebra and vector spaces, but maybe this image helps to give some intuition:

https://commons.wikimedia.org/wiki/File:Matrix_multiplication_diagram_2.svg

Or maybe an example?

Matrix multiplication can be used to solve linear equations:`8x+3y=3`

3x+y=1

[ 8 3 ] x [ x ] = [ 3 ]

[ 3 1 ] [ y ] [ 1 ]

To balance the equation out (do the same stuff to both sides), you multiply both sides by the*inverse*of the first matrix to cancel it out on the left side (i've taken an extremely simple-to-inverse example on purpose, inversing is pretty complicated for many other matrices, often indicating that your system of equations is not going to have nice integers as answers):`[ -1 3 ] x [ 8 3 ] x [ x ] = [ -1 3 ] x [ 3 ]`

[ 3 -8 ] [ 3 1 ] [ y ] [ 3 -8 ] [ 1 ]

So that results in:`[ x ] = [ -1 3 ] x [ 3 ]`

[ y ] [ 3 -8 ] [ 1 ]

Which simplifies (thanks to our "weird" definition of multiplication) to :`[ x ] = [ 0 ]`

[ y ] [ 1 ]

Which happens to be the answers to our system of linear equations!(113 votes)

- Am I just supposed to memorize these steps....it doesn't make any sense ..someone please explain its applications...(11 votes)
- Rajeswari,

Multiplying matrices is useful in lots of engineering applications, but the one that comes to my mind is in computer graphics. You can think of a point in three dimensional space as a 1 by 3 matrix, where the x coordinate is the 1,1 value in the matrix, y is the 1,2 and the z coordinate is the 1,3 value. Like this: [x y z]

Well, imagine you have a collection of points that draw something on a computer screen. For example, let's say you have 1000 points, and when you draw lines from point 1 to 2 to 3 and so on all the way to 1000, it draws some shape… maybe a picture of a dog or a train or something. If take the 3 coordinates from each point and make them a row of a matrix, you would have a 1000 by 3 matrix:

|x1 y1 z1|

|x2 y2 z2|

|x3 y3 z3|

|… … … |

|x1000 y1000 z1000|

Well, once you've got that 1000 by 3 matrix, there are very easy ways to manipulate it using matrix multiplication. Say you want to make your train 3 times bigger in the x direction and 1/2 the size in the y direction. Well you would multiply your 1000 by 3 matrix times the matrix:

| 3 0 0 |

| 0 1/2 0 |

| 0 0 1 |

If you did this, you would get a new 1000 by 3 matrix, and if you plotted those points, your picture of your dog or train or whatever would be 3 times bigger in the x direction and half the size in the y direction.

This is the kind of math that a lot of computer graphics programs are doing all the time. They are doing constant matrix multiplication on a list of points to compute a new list of points that describe what the object would look like if you zoomed in or out, or rotated it or whatever.(54 votes)

- I still did not understand the concept of "dot product". If the term is not important, maybe it could be left out from the video - and if it is important, perhaps it should be explained shortly.(25 votes)
- The dot product is an operation between two vectors. All you do is take the components of each vector, multiply them together, and add it up. Vectors can be thought of as matrices with just one row or column. Example:

v = [0, 1, 2]

w = [2, 4, 1]

With these two vectors, the dot product is:

v . w = (0)(2) + (4)(1) + (2)(1) = 6

So as you can see, matrix multiplication is basically doing this for each row in the matrix, that's why Sal mentioned it. I think he should go over vectors before matrices in the algebra playlist to fix this confusion, but for the time being he does go over vectors in the physics and linear algebra playlists.(13 votes)

- is it possible to take a certain matrix A to the power of another Matrix B? If so how would you take a matrix to a power which is another matrix?

A = [3,1,5]

[2,4,6]

[7,8,9]

B = [5,4,6]

[7,1,2]

[3,4,9]

A^B = x(this could be a matrix or a scalar)(11 votes)- No, this is not a defined operation. That means, basically, there is no way to do it that (a) works for all matrices, (b) gives meaningful, consistent results. Certainly it would not be possible to define it in terms of (matrix) multiplication.(5 votes)

- What is the difference between a matrix and a vector?(6 votes)
- A vector is a special matrix. A vector is an n by 1 matrix, or a 1 by n matrix, where you replace n by an integer. For example:

[1 5 2] is a 1 by 3 matrix, it is also a vector because one of its dimensions is one.(11 votes)

- *Could you multiply matrices if their dimensions are the same like 3x2 and 3x2 or do the dimensions have to be opposite?*(3 votes)
- When you list the dimensions of the matrices you are going to multiply, the middle two numbers must be the same, so a 4x2 times a 2x5 works (the result would be 4x5) and a 3x2 times a 3x2 won't.(8 votes)

- I am trying to work my way up to 2x2 without adding vectors. Is this valid?

First is [ a b ]

Second is [ c

......................d ] Meets the that columns of first = rows of second. Result is [ ac+bd ] I think. So then...

First is still [ a b ]

Second is now [ c e

..............................d f ] Added a column and columns of first = rows of second so results is

[ ac+eb bd+bf] ?

Or is it [ ac+ad be+bf] ?

Or [ ac+bd ae+bf ] ? This seems most likely as 1,1 is the same.

Or is this just illegal operation.(5 votes)- Matrix multiplication is only valid if the number of columns of the first matrix are equal to the number of rows of the second matrix; further, the resulting matrix will have the number of rows of the first matrix and the number of columns of the second matrix.

For example, if you multiply a 1⨉2 matrix by a 2⨉3 matrix (you can do the multiplication, since the first matrix has 2 columns and the second matrix has 2 rows), then the resulting matrix will be a 1⨉3 matrix.

Switching the matrices, you cannot multiply a 2⨉3 matrix by a 1⨉2 matrix, since the number of columns in the first one (3) is not equal to the number of rows in the second one(1).

Now, while multiplying, for every item in the resulting matrix, you have to multiply a whole row of the first matrix with a whole column of the second matrix, element by element and add them.

For example, here's a 1⨉3 times 3⨉2 matrix multiplication, with the 1⨉2 result:`⎡d e⎤`

[ a b c ] ⎢f g⎥ = [ ad+bf+ch ae+bg+ci ]

⎣h i⎦

And here's an example of a 2⨉2 times 2⨉2 matrix, and the resulting 2⨉2:`⎡ a b ⎤⎡ e f ⎤ ⎡ ae+bg af+bh ⎤`

⎢ ⎥⎢ ⎥ = ⎢ ⎥

⎣ c d ⎦⎣ g h ⎦ ⎣ ce+dg cf+dh ⎦

Finally, here's a 2⨉1 times a 1⨉2, and the resulting 2⨉2:`⎡a⎤ ⎡ac ad⎤`

⎢ ⎥[ c d ] = ⎢ ⎥

⎣b⎦ ⎣bc bd⎦(4 votes)

- At0:57Sal says that the first thing that we need to do is see if "it is even a valid operation." What does that mean?(4 votes)
- You need to check the dimensions of the matrices being multiplied. You can only multiply matrices if the number of columns of the first matrix is the same as the number of rows as the second matrix.

For example, say you want to multiply A x B. If A is a 3x1 matrix, B has to be a 1xY matrix (Y can be any number), because A only has 1 column.

This also leads to an interesting result, by the way. A x B may be defined, whereas B x A may not be defined.(3 votes)

- How can you multiply a 2x3 matrix by a 4x1 matrix ?(4 votes)
- Because you're taking the dot product of the 1st matrix's row and the 2nd matrix's column, it requires the 1st matrix's rows to have the same amount of entries as the 2nd matrix's columns, or some entry will be "left hanging", with no friend from the other matrix to be multiplied with.

Therefore, we call such an operation undefined, kind of like how anything divided by zero is undefined.

i.e. you can't multiply them with one other unless you make up some specific new rule on how to do it

Just to elaborate and give clear-worded rules:

If we want to: AxB

then A's column number must be equal to B's row number

i.e. if A's dimensions are j x k, and B's dimensions are m x n

k must equal m, if you want to AxB(2 votes)

- How do I know what the dimensions will be of a product matrix, if the two multiplied matrices have different dimensions?(2 votes)
- Given two matrices A and B where the dimension of A is m x n and the dimension of B is n x r, then the matrix AB will have dimension m x r. Take note that for two matrices to be multiplied the number of columns in the first matrix must equal the number of rows in the second matrix... if this is not so then multiplication is impossible. Take note also that this means matrix multiplication is not commutative... for matrices order matters.(5 votes)

## Video transcript

We're given two matrices over
here, matrix E and matrix D. And they ask us,
what is ED, which is another way of saying
what is the product of matrix E and matrix D? Just so I remember
what I'm doing, let me copy and paste this. And then I'm going to get
out my little scratch pad. So let me paste that over here. So we have all the
information we needed. And so let's try
to work this out. So matrix E times matrix D,
which is equal to-- matrix E is all of this business. So it is 0, 3, 5, 5, 5, 2 times
matrix D, which is all of this. So we're going to multiply
it times 3, 3, 4, 4, negative 2, negative 2. Now the first thing
that we have to check is whether this is
even a valid operation. Now the matrix multiplication
is a human-defined operation that just happens-- in
fact all operations are-- that happen to have
neat properties. Now the way that us humans have
defined matrix multiplication, it only works when we're
multiplying our two matrices. So this right over here has
two rows and three columns. So it's a 2 by 3 matrix. And this has three rows and
two columns, it's 3 by 2. This only works-- we
could only multiply this matrix times this matrix,
if the number of columns on this matrix is equal to the
number of rows on this matrix. And in this situation it is, so
I can actually multiply them. If these two numbers weren't
equal, if the number of columns here were not equal to the
number of rows here, then this would not be a valid
operation, at least the way that we have defined
matrix multiplication. The other thing you
always have to remember is that E times D is not always
the same thing as D times E. Order matters when you're
multiplying matrices. It doesn't matter if you're
multiplying regular numbers, but it matters for matrices. But let's actually
work this out. So what we're going to get
is actually going to be a 2 by 2 matrix. But I'm going to
create some space here because we're going to have
to do some computation. So this is going to
be equal to-- I'm going to make a huge
2 by 2 matrix here. So the way we get
the top left entry, the top left entry
is essentially going to be this row
times this product. If you view them
each as vectors, and you have some familiarity
with the dot product, we're essentially
going to take the dot product of that and that. And if you have no idea what
that is, I'm about to show you. This entry is going to be
0 times 3, plus 3 times 3, plus 5 times 4. So that is the top left entry. And I already see that I'm going
to run out of space here, so let me move this
over to the right some space so I have
some breathing room. Now we can do the
top right entry. This was the top left, now
we're going to do the top right. So the top right
entry is going to be this row times this column. Notice the entry
is getting the row from the first matrix and the
column from the second one. That's kind of
determining its position. So, once again, is
going to be 0 times 4, plus 3 times negative 2,
plus 5 times negative 2. And we keep going. The bottom left entry is going
to be this row, the second row here times the
first column here. So it's 5 times 3, plus 5
times 3, plus 2 times 4. And we're almost done. We just need to multiply or
take the dot product of this row with this column
right over here. So it's going to be 5 times
4, plus 5 times negative 2, plus 2 times negative 2. And so this is going
to be equal to, and we could just
evaluate this now. Let's see, 0 times 3 is 0. This is 9 plus 20. This is 29. This all simplified to 29. All of this is 0. This is negative 6. And then this is minus 10. So this all simplifies
to negative 16. This right over here, it's 15
plus 15, which is 30 plus 8. So this is 38. And then finally, this
is 20 minus 10 minus 4. So this is going to be 6. So this is all going
to simplify to 6. So this all became 29,
negative 16, 38, and 6. Let's check our answer. And we got it right.