If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### Course: Precalculus>Unit 7

Lesson 10: 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.
• 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=33x+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!
• Am I just supposed to memorize these steps....it doesn't make any sense ..someone please explain its applications...
• 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.
• 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.
• 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.
• 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)
• 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.
• What is the difference between a matrix and a vector?
• 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.
• *Could you multiply matrices if their dimensions are the same like 3x2 and 3x2 or do the dimensions have to be opposite?*
• 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.
• 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.
• 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⎦``
• At Sal says that the first thing that we need to do is see if "it is even a valid operation." What does that mean?
• 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.
• How can you multiply a 2x3 matrix by a 4x1 matrix ?
• 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