[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, 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 = X + Y, what's Z look like? 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 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 + 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. Let's let X = 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 6 elements. Y is 1 by 6, also 6 elements. So let's add them and assign them to Z. MATLAB doesn't like this, says matrix dimensions must agree. Well, we've tried to add two by three matrix and a one by six matrix. And as we pointed out, that doesn't work. So what I meant to do was make Y a two by three matrix by putting a semi-colon here instead of a comma. And now I'm going to use the up arrow to repeat that command, Z = X + Y, and things work just fine now. Subtraction works great too, let's try that. Let's let Z = X- 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, it 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 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'll 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 .* operator, so we say Z = X .* 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 minus 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 = X.*Y, that's array multiplications. 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 7 and 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. There, so it's a two-by-three array. You'd probably have a larger array in 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. 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 the highs measured by the corresponding element in the 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. .*, 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, they're 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. Well, 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 out 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 M's. Because of the way that these M's are situated between the L and the N, they're called the inner dimensions of X and Y. And that provides a simply 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 effect 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 it's 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. Ranging their dimensions like this shows that the inner dimensions are equal. Here are the inner dimensions right here, 3 and 3, which is another way to say the dimensions satisfy the rule for matrix multiplication. So let's multiply them and assign their 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, 0. 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. To calculate it like this, now I'm going to write it out in detail. There it is, and indeed it equals zero. 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 for these 4. 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's performed for every row of A with every column of B. And since there are 4 rows of A and 2 columns of B, we get a 4x2 matrix. And what happens if the inner 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 use the same two matrices, A and B, and here they are again, A, B. Suppose a given 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 #1 owns 1 share of the first stock, 2 shares of the second stock, and 3 shares of the third stock. Person #2 owns 4, 5, and 6 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 a second day. So looking at column one we see that on the first day the first stock gained $2, second stock gained $3, and 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 of B. Well that's exactly what matrix multiplication does for us, so let's do it. There are four people and two days, so there are eight distinct profits to calculate and the results are all laid out neatly in matrix C. 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 and books on this subject. [MUSIC] [NOISE] [APPLAUSE]