0:00

In this video, I'd like

Â to start talking about how to

Â multiply together two matrices.

Â We'll start with a special case

Â of that, of matrix vector

Â multiplication - multiplying a matrix together with a vector.

Â Let's start with an example.

Â Here is a matrix,

Â and here is a vector, and

Â let's say we want to

Â multiply together this matrix

Â with this vector, what's the result?

Â Let me just work through this

Â example and then we

Â can step back and look at just what the steps were.

Â It turns out the result of

Â this multiplication process is going

Â to be, itself, a vector.

Â And I'm just going work

Â with this first and later we'll

Â come back and see just what I did here.

Â To get the first element of

Â this vector I am going

Â to take these two numbers

Â and multiply them with

Â the first row of the

Â matrix and add up the corresponding numbers.

Â Take one multiplied by

Â one, and take

Â three and multiply it by

Â five, and that's

Â what, that's one plus fifteen so that gives me sixteen.

Â I'm going to write sixteen here.

Â then for the second row,

Â second element, I am

Â going to take the second row

Â and multiply it by this vector,

Â so I have four

Â times one, plus zero

Â times five, which is

Â equal to four, so you'll have four there.

Â And finally for the last

Â one I have two one times

Â one five, so two

Â by one, plus one

Â by 5, which is equal

Â to a 7, and

Â so I get a 7 over there.

Â 1:43

It turns out that the

Â results of multiplying that's

Â a 3x2 matrix by a

Â 2x1 matrix is also

Â just a two-dimensional vector.

Â The result of this is

Â going to be a 3x1

Â matrix, so that's why

Â three by one 3x1

Â matrix, in other words

Â a 3x1 matrix is just a three dimensional vector.

Â So I realize that I

Â did that pretty quickly, and you're

Â probably not sure that you can

Â repeat this process yourself, but

Â let's look in more detail

Â at what just happened and what

Â this process of multiplying a matrix by a vector looks like.

Â Here's the details of how to

Â multiply a matrix by a vector.

Â Let's say I have a matrix A

Â and want to multiply it by

Â a vector x. The

Â result is going to be some

Â vector y. So the

Â matrix A is a m

Â by n dimensional matrix, so

Â m rows and n columns and

Â we are going to multiply that by a

Â n by 1 matrix, in other words an n dimensional vector.

Â It turns out this

Â "n" here has to match this "n" here.

Â In other words, the number of

Â columns in this matrix, so

Â it's the number of n columns.

Â The number of columns here has

Â to match the number of rows here.

Â It has to match the dimension of this vector.

Â And the result of this product

Â is going to be an n-dimensional

Â vector y. Rows here.

Â "M" is going

Â to be equal to the number

Â of rows in this matrix "A".

Â So how do you actually compute this vector "Y"?

Â Well it turns out to compute

Â this vector "Y", the process

Â is to get "Y""I", multiply "A's"

Â "I'th" row with the

Â elements of the vector "X"

Â and add them up.

Â So here's what I mean.

Â In order to get the

Â first element of "Y",

Â that first number--whatever that turns

Â out to be--we're gonna take

Â the first row of the

Â matrix "A" and multiply

Â them one at a time

Â with the elements of this vector "X".

Â So I take this first number

Â multiply it by this first number.

Â Then take the second number multiply it by this second number.

Â Take this third number whatever

Â that is, multiply it the third number

Â and so on until you get to the end.

Â And I'm gonna add up the

Â results of these products and the

Â result of paying that out is going to give us this first element of "Y".

Â Then when we want to get

Â the second element of "Y", let's say this element.

Â The way we do that is we

Â take the second row of

Â A and we repeat the whole thing.

Â So we take the second row

Â of A, and multiply it

Â elements-wise, so the elements

Â of X and add

Â up the results of the products

Â and that would give me the

Â second element of Y. And

Â you keep going to get and we

Â going to take the third row

Â of A, multiply element Ys

Â with the vector x,

Â sum up the results and then

Â I get the third element and so

Â on, until I get down

Â to the last row like so, okay?

Â 4:55

So that's the procedure.

Â Let's do one more example.

Â Here's the example: So let's look at the dimensions.

Â Here, this is a three

Â by four dimensional matrix.

Â This is a four-dimensional vector,

Â or a 4 x 1 matrix, and

Â so the result of this, the

Â result of this product is going

Â to be a three-dimensional vector.

Â Write, you know, the vector,

Â with room for three elements.

Â Let's do the, let's carry out the products.

Â So for the first element, I'm

Â going to take these four numbers

Â and multiply them with the

Â vector X. So I have

Â 1x1, plus 2x3,

Â plus 1x2, plus 5x1, which

Â is equal to - that's

Â 1+6, plus 2+6, which gives me 14.

Â And then for the

Â second element, I'm going

Â to take this row now and

Â multiply it with this vector (0x1)+3.

Â All right, so

Â 0x1+ 3x3 plus

Â 0x2 plus 4x1,

Â 6:20

which is equal to, let's

Â see that's 9+4, which is 13.

Â And finally, for the last

Â element, I'm going to take

Â this last row, so I

Â have minus one times one.

Â You have minus two, or really there's a plus next to a two I guess.

Â Times three plus zero

Â times two plus zero times

Â one, and so that's

Â going to be minus one minus

Â six, which is going to make

Â this seven, and so that's vector seven.

Â Okay?

Â So my final answer is this

Â vector fourteen, just to

Â write to that without the colors, fourteen,

Â thirteen, negative seven.

Â 7:01

And as promised, the

Â result here is a three by one matrix.

Â So that's how you multiply a matrix and a vector.

Â I know that a lot just

Â happened on this slide, so

Â if you're not quite sure where all

Â these numbers went, you know,

Â feel free to pause the video

Â you know, and so take a

Â slow careful look at this

Â big calculation that we just

Â did and try to make

Â sure that you understand the steps

Â of what just happened to get

Â us these numbers,fourteen, thirteen and eleven.

Â Finally, let me show you a neat trick.

Â Let's say we have

Â a set of four houses so 4

Â houses with 4 sizes like these.

Â And let's say I have a

Â hypotheses for predicting what is

Â the price of a house, and

Â let's say I want to compute,

Â you know, H of X for each of my 4 houses here.

Â It turns out there's neat way

Â of posing this, applying this

Â hypothesis to all of my houses at the same time.

Â It turns out there's a neat

Â way to pose this as a

Â Matrix Vector multiplication.

Â So, here's how I'm going to do it.

Â I am going to construct a matrix as follows.

Â My matrix is going to be

Â 1111 times, and I'm

Â going to write down the sizes

Â of my four houses here and

Â I'm going to construct a vector

Â as well, And my

Â vector is going to this

Â vector of two elements, that's

Â minus 40 and 0.25.

Â That's these two co-efficients;

Â data 0 and data 1.

Â And what I am going

Â to do is to take matrix

Â and that vector and multiply them

Â together, that times is that multiplication symbol.

Â So what do I get?

Â Well this is a

Â four by two matrix.

Â This is a two by one matrix.

Â So the outcome is going

Â to be a four by one

Â vector, all right.

Â So, let me,

Â so this is

Â going to be a 4 by

Â 1 matrix is the outcome or

Â really a four diminsonal vector,

Â so let me write it as

Â one of my four elements in my four real numbers here.

Â Now it turns out and so

Â this first element of this

Â result, the way I

Â am going to get that is, I

Â am going to take this and multiply it by the vector.

Â And so this is going to

Â be -40 x

Â 1 + 4.25 x 2104.

Â By the way, on

Â the earlier slides I was

Â writing 1 x -40 and

Â 2104 x 0.25, but

Â the order doesn't matter, right?

Â -40 x 1 is the same as 1 x -40.

Â And this first element, of course,

Â is "H" applied to 2104.

Â So it's really the

Â predicted price of my first house.

Â Well, how about the second element?

Â Hope you can see

Â where I am going to get the second element.

Â Right?

Â I'm gonna take this and multiply it by my vector.

Â And so that's gonna be

Â -40 x 1 + 0.25 x 1416.

Â And so this is going be "H" of 1416.

Â Right?

Â 10:25

And so on for the

Â third and the fourth

Â elements of this 4 x 1 vector.

Â And just there, right?

Â This thing here that I

Â just drew the green box around,

Â that's a real number, OK?

Â That's a single real number,

Â and this thing here that

Â I drew the magenta box around--the

Â purple, magenta color box

Â around--that's a real number, right?

Â And so this thing on

Â the right--this thing on the

Â right overall, this is a

Â 4 by 1 dimensional matrix, was a 4 dimensional vector.

Â And, the neat thing about

Â this is that when you're

Â actually implementing this in software--so

Â when you have four houses and

Â when you want to use your hypothesis

Â to predict the prices, predict the price "Y" of all of these four houses.

Â What this means is that, you

Â know, you can write this in one line of code.

Â When we talk about octave and

Â program languages later, you can

Â actually, you'll actually write this in one line of code.

Â You write prediction equals my,

Â you know, data matrix times

Â 11:30

parameters, right?

Â Where data matrix is

Â this thing here, and parameters

Â is this thing here, and this

Â times is a matrix vector multiplication.

Â And if you just do this then

Â this variable prediction - sorry

Â for my bad handwriting - then

Â just implement this one

Â line of code assuming you have

Â an appropriate library to do matrix vector multiplication.

Â If you just do this,

Â then prediction becomes this

Â 4 by 1 dimensional vector, on

Â the right, that just gives you all the predicted prices.

Â And your alternative to doing

Â this as a matrix vector multiplication

Â would be to write eomething like

Â , you know, for I equals 1 to 4, right?

Â And you have say a thousand houses

Â it would be for I equals 1 to a thousand or whatever.

Â And then you have to write a

Â prediction, you know, if I equals.

Â and then do a bunch

Â more work over there and it

Â turns out that When you

Â have a large number of houses,

Â if you're trying to predict the prices

Â of not just four but maybe

Â of a thousand houses then

Â it turns out that when

Â you implement this in the

Â computer, implementing it like this, in any of the various languages.

Â This is not only true for

Â Octave, but for Supra Server

Â Java or Python, other high-level, other languages as well.

Â It turns out, that, by writing

Â code in this style on the

Â left, it allows you to

Â not only simplify the

Â code, because, now, you're just

Â writing one line of code

Â rather than the form of a bunch of things inside.

Â But, for subtle reasons, that we

Â will see later, it turns

Â out to be much more computationally

Â efficient to make predictions

Â on all of the prices of

Â all of your houses doing it

Â the way on the left than the

Â way on the right than if you were to write your own formula.

Â I'll say more about this

Â later when we talk about

Â vectorization, but, so, by

Â posing a prediction this way, you

Â get not only a simpler piece

Â of code, but a more efficient one.

Â So, that's it for

Â matrix vector multiplication and we'll

Â make good use of these sorts

Â of operations as we develop

Â the living regression in other models further.

Â But, in the next video we're

Â going to take this and generalize this

Â to the case of matrix matrix multiplication.

Â