Lesson 2.5: Arithmetic Part 1 (Old Version)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[MUSIC] We're going to look at array addition. There are special rules for arithmetic on arrays. Let's look at an example to illustrate it. X and Y are two arrays that we want to add together. X is a 3 by 2 matrix, 1, 4, 7, 0, 5, 5. And Y is also a 3 by 2 matrix, 2, -4, 6, 2, 0, and 3. In order to add two matrices together, we have to make sure that they have the same dimensions. So suppose we give MATLAB the command Z equals X plus Y. What's Z look like? Well here's what X plus Y equals to Z means. On the right side, we see the two matrices X and Y added together, and on the left side, we see what happens. Let's look at the first element on the first row. The first element on the first row of X and Y is 1 and 2. We add those together: 1 plus 2. That's what we put at the first element on the first row of Z. Let's look at the next element on that row. X and Y are 4 and -4. If we add those together, we get 4 plus -4, which will be 0. And we do this for every position in the matrices. If we carry out these individual additions, we get 3, 0, 13, 2, 5 and 8 on the left side, and so Z is equal to 3, 0, 13, 2, 5, and 8. Let’s take a look at this in MATLAB. We'll give MATLAB something to work with. So let X equal 1, 5, -2 on the first row, and then 3, 0, 7 on the second row. And we'll be lazy with Y. We'll use the colon operator. So X is 2 by 3. That's six elements. Y is 1 by 6, also six elements. So let's add them and assign them to Z. [CLICKING] Ah! MATLAB doesn't like this. It says, “Matrix dimensions must agree.” Well we've tried to add a 2 by 3 matrix and a 1 by 6 matrix, and as we pointed out, that doesn't work. So what I meant to do was make Y a 2 by 3 matrix. By putting a semicolon here instead of a comma, and now I'm going to use the up-arrow to repeat that command. Z equals X plus Y, and things work just fine now. Subtraction works great too. Let's try that. Let's let Z equal X minus Y. And there's no problem there either. Let's look at some of these numbers and see if they came out right. With addition, well let's look at this second element on the first row. We ended up with a 7. Where'd that come from? Well, there was a 5 at that position in X and a 2 in Y. 5 plus 2 is 7. And what about this -5 on the second row of Z here, when we did subtraction? Well, we had a 0 there for X, and we had a 5 for a Y. 0 minus 5 gives us -5. And we can do a similar thing with array multiplication. Array multiplication is very similar to array addition. We use the same example, X and Y, that we used to illustrate addition, for multiplication. If we want to do array multiplication, we use the dot asterisk operator. So we say Z equals X dot asterisk Y. You may wonder why we don't just use the regular asterisk, which is the traditional symbol for times. That's because the asterisk alone is reserved for matrix multiplication, which is different from array multiplication. So what does Z look like? Well, on the right side, you see the same numbers you saw for array addition, but this time the operator is the dot star operator, and on the left the only difference is: instead of plus everywhere you see the asterisk, the times symbol. So we have 1 times 2, 4 times -4, and so on. So you carry out all these multiplications, and you can see the result for Z right here. Well, array multiplication has the same rule that addition and subtraction has, that is that the matrices that are operands have to have the same dimensions. So we'll use the same examples, X and Y, that we used for addition and subtraction, and here we go. Z is equal to X dot star Y. That's array multiplication. Really simple, and we can check an example. Here's a 5 and a 2 on the first row second element, and 5 times 2 is 10. And if we look at the last element on the second row, we see a 7 and a 6, and 7 times 6 is 42. So when might you use array multiplication? Well, let's clear the screen and do an example. Let's suppose you had an array of temperature sensors, outdoors somewhere, laid out in a rectangular grid, covering a few square miles and reporting back to a central facility. And today, let's suppose they measured these high temperatures which I've already put into an array called Highs_measured. Let's take a look at these. [CLICKING] There. So it's a 2 by 3 array. You'd probably have a larger array in a real application, but this is just an example. And furthermore, suppose that each sensor's measured temperature must be corrected by multiplying it by the sensor's calibration factor. And that would be kept in a permanent array of the same shape. I've set up that array too and called it Calibration_factors. Let's look at that one. [CLICKING] There. And you can see both of these over in the workspace over here on the right. They were sitting there. I don't know if you’d noticed them. Well, to get the true temperatures, we now need to multiply each element in Highs_measured by the corresponding element in Calibration_factors. And that's exactly what array multiplication does for us, so let's do that. Let's call the result Highs_true. And we'll set that to Highs_measured dot star …There's that array multiplication operator… Calibration_factors, and there's the result. If you look back at the original temperatures up here, 71, 52, 78, and 83 and so on, these are more reasonable down here, 78, 78, 77, and just varying by a smaller amount. They're more reasonable because you wouldn't expect a wide variation over just a few square miles. The need for calibration like this is fairly common, and array multiplication makes it easy to do. So now we know how array multiplication works, and we know that matrix multiplication is very different from array multiplication. In fact it differs in three ways. First there's obviously no dot. Second, the shapes of X and Y must be compatible, which is different from having the same shape. And third, the calculation of each element of Z uses both multiplication and addition. But first let's look at the compatibility requirement. Here we show Z, X, and Y schematically. And we've been careful to give them all different shapes. So X is clearly not the same shape as Y, but it is as we'll see compatible with Y. X and Y are compatible because the width of X is the same as the height of Y. We've highlighted that with the blue arrows. To be specific, the width of the left operand of the matrix multiplication operator must be equal to the height of the right operand. Put another way, the number of columns of the left operand X must equal the number of rows of the right operand Y. Let's move our schematic up a little bit to make room to write their dimensions, and there are X's dimensions. And we've color-coded them to make it easy to see that the number of rows, L, is the height of X. And the number of columns, M, is its width. Similarly, we show the dimensions of Y and color-code them, so it's obvious that the number of rows, M, of Y is its height, and the number of columns, N, is its width. If we drop the schematics and put the dimensions under X and Y in the matrix multiplication operation, we notice that there are two dimensions next to each other that are equal, the two blue Ms. Because of the way that these Ms are situated between the L and the N, they're called the “inner” dimensions of X and Y. And that provides a simple way to state the compatibility rule for matrix multiplication. We simply say that the inner dimensions of X and Y must be equal. So, what about the L and the N? Well, they're called the “outer” dimensions, and there's no restriction on them whatever, but they do have an affect on the calculation. They determine the shape of the result. Z will be an L by N matrix. So to review, the width of X, blue, must equal the height of Y. The height of Z, red, equals the height of X. And the width of Z, green, equals the width of Y. So, we know the shape of Z, but we still don't know how its elements are calculated. Let's use MATLAB to see how that's done. I've already prepared a couple of matrices that we can multiply: A and B. The dimensions are 4 by 3 for A and 3 by 2 for B. Since the width of A is equal to the height of B, we can do matrix multiplication of A times B. But before we do, let's put the sizes into a row vector, like this. Arranging their dimensions like this shows that the inner dimensions are equal. Here are the inner dimensions right here, 3, 3. Which is another way to say that the dimensions satisfy the rule for matrix multiplication. So let's multiple them and assign the result to C. So C is 4 by 2, and those are the outer dimensions, 4 and 2, of the matrices A and B, as expected. Okay, so let's see how matrix multiplication produced an element of C, say the 0 on the third row. That element is C(3,2), this one. There, zero. Since C(3,2) is on row 3 and column 2, its calculation involves only the elements on row 3 of A and column 2 of B. It’s calculated like this. Now I'm going to write it out in detail. There it is, and indeed it equals 0. Now let's look at that expression. Note that the row index stays the same for A, so, 3, 3, 3. And the column index stays the same for B. So it's 2, 2, and 2. But the inner index, this one here, this 1 and this 1, the inner index means it's the inner index of these four, and it's the same for the A and the B. It ranges from 1 to 2 to 3. It ranges across the entire width of A and height of B. The products of these pairs of elements are then summed to get element 3, 2 of C. This same sort of calculation is performed for every row of A with every column of B. And, and since there are four rows of A and two columns of B, we get a 4 by 2 matrix. And what happens if the inter dimensions do not match? Well we can get an example of that by just multiplying B and A in the other order. MATLAB complains. It says “Inner matrix dimensions must agree.” And to show that it's right to complain, let's put the dimensions for B times A into a row vector, just as we did for A times B. So, size B this time, size A. Sure enough, the inner dimensions, 2 and 4, are not equal. So MATLAB's right. But then MATLAB's always right. So that's how matrix multiplication is carried out. But when might you want to use matrix multiplication? Well, let's clear the screen. There! Nice and tidy, and look at an application. We'll use the same two matrices, A and B, and here they are again, A, B. Suppose a given a row of A represents numbers of shares of each of three stocks. And each row gives the shares owned by one of four persons. So person number 1 owns one share of the first stock, two shares of the second stock, and three shares of the third stock. Person 2 owns four, five, and six shares respectively, and so on. Suppose also that each column of B represents the gain in dollars of three stocks. Column one gives the gains for the stocks on one day, and column two gives the gains on the second day. So looking at column one, we see that on the first day the first stock gained $2, the second stock gained $3 and the third one gained $7. Looking at column two, we see that on the second day the first stock lost $2 and the other two gained 8 and $4 respectively. Well to calculate the profit that one person would have made on a given day, we need to multiply the number of shares times the gain for each stock on that day and then add the three products together. So for example to calculate the profit for the first person on the first day, we would multiply one share times $2, to get $2 and two shares times $3, to get $6 and three shares times $7 to get $21 and then add these three products together to get $29. Then we'd repeat that process for each person . . . that's each row of A . . . and each day . . . that's each column B. Well that's exactly what matrix multiplication does for us. So let's do it. [CLICKING]. There are four people in two days, so there are eight distinct profits to calculate, and the results are all laid out neatly in matrix C. Hm, let's see. The first person profited about the same on each day, $29 and $26. The third person did much better on the first day: Didn't make anything on the second day, and by comparing the columns, we can see that everybody did better on the first day than they did on the second day. Matrix multiplication made this calculation easy, but this operator has applications far beyond the stock market and far beyond finance. The applications fall under the heading of linear algebra, and you can find many examples in books on this subject. [MUSIC] [APPLAUSE]
Info
Channel: Fitzle LLC
Views: 34,837
Rating: 4.9718308 out of 5
Keywords: MATLAB (Programming Language)
Id: p7sBHhmdVf0
Channel Id: undefined
Length: 17min 49sec (1069 seconds)
Published: Sat Jun 13 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.