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.

Main content

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?

  • aqualine ultimate style avatar for user Mi Pob
    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.
    (38 votes)
    Default Khan Academy avatar avatar for user
    • duskpin ultimate style avatar for user okdewit
      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!
      (108 votes)
  • leaf green style avatar for user Rajeswari Murukan
    Am I just supposed to memorize these steps....it doesn't make any sense ..someone please explain its applications...
    (11 votes)
    Default Khan Academy avatar avatar for user
    • male robot donald style avatar for user Jeremy
      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)
  • aqualine ultimate style avatar for user Miia
    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.
    (23 votes)
    Default Khan Academy avatar avatar for user
    • piceratops ultimate style avatar for user Tom Brannan
      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.
      (11 votes)
  • female robot grace style avatar for user Anna
    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)
    Default Khan Academy avatar avatar for user
    • male robot hal style avatar for user Ben Willetts
      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.
      (4 votes)
  • male robot hal style avatar for user jyuan
    What is the difference between a matrix and a vector?
    (6 votes)
    Default Khan Academy avatar avatar for user
  • marcimus pink style avatar for user ส็็็็็็็็็็็็็็็็็็็ sholmes ส้้้้้้้้้้้้้้้้้้้้
    *Could you multiply matrices if their dimensions are the same like 3x2 and 3x2 or do the dimensions have to be opposite?*
    (3 votes)
    Default Khan Academy avatar avatar for user
  • leafers ultimate style avatar for user Eric Chipko
    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)
    Default Khan Academy avatar avatar for user
    • male robot hal style avatar for user Yamanqui García Rosales
      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)
  • piceratops ultimate style avatar for user YipChunSang
    How can you multiply a 2x3 matrix by a 4x1 matrix ?
    (4 votes)
    Default Khan Academy avatar avatar for user
    • leafers tree style avatar for user Erik Mingjun Ma
      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)
  • mr pants teal style avatar for user Anthony
    How do I know what the dimensions will be of a product matrix, if the two multiplied matrices have different dimensions?
    (2 votes)
    Default Khan Academy avatar avatar for user
    • blobby green style avatar for user DeWain Molter
      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)
  • hopper cool style avatar for user 🤔 ᴄᴏᴅᴇᴅ ɢᴇɴɪᴜȿ 😎
    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?
    (3 votes)
    Default Khan Academy avatar avatar for user
    • piceratops ultimate style avatar for user Fai
      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)

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.