0:58

So the format we're going to use for these first, these slides in this first lecture

Â are, I'm going to show some text that

Â was generated using my own MATLAB command window.

Â And we're going to start by talking, telling you how

Â to define variables in different forms and simple arithmetic.

Â And the, syntax that we're going to use is

Â that MATLAB commands are going to be shown in

Â black Courier font, so these were MATLAB commands

Â that were typed into my MATLAB command window.

Â And then have been pasted here into the power point.

Â And then, where I'm going to illustrate

Â some comments on this, where I'm going to explain

Â some things about these MATLAB commands, that's going

Â to be shown in the red Arial font.

Â So if you follow along the, the different

Â fonts, you can tell whether something came directly

Â from MATLAB or whether it's just some, some

Â comments and some annotation on those MATLAB commands.

Â 2:20

For instance c equals a plus b.

Â C will be 7.

Â Or d equals a times b, then d will of course be equal to 12.

Â Or instead of using, defining d by multiplying, you can define d equals

Â a divided by b, and in this case d will be equal to 1.333.

Â One of the things that you'll notice

Â if you're familiar with programming languages is

Â that, a and b do not have, you know, because we've defined them as

Â integer values, 4 and 3, we can still perform a division here, and MATLAB

Â will automatically, keep track of this and

Â say okay, well these are no longer integers.

Â Now, d is equal to something that has a, has a fraction associated with it.

Â And these comp commands to add, multiply, et cetera are all variant to what if.

Â 3:31

You could type in a equals, and you'd have

Â to have this square bracket in the beginning and

Â a square bracket at the end and then in between you'd type 1 comma 2 comma 3 etc.

Â All the numbers up to 10.

Â And now a, instead of just being equal to four like it was in

Â the last slide, now a is equal to all the numbers from 1 to 10.

Â [BLANK_AUDIO]

Â You could get the same answer if you typed in a equals one colon, comma.

Â One colon ten.

Â So what that does is it has evenly spaced

Â integers from whereever you start up to wherever you end.

Â See all the numbers from one to ten, just like you had up

Â here, but you didn't have to type in every single one of the values.

Â Now you say, what if you wanted to, if you didn't want them to be spaced, with a

Â spacing of one, what if you just wanted odd

Â numbers, or what if you just wanted even numbers?

Â 6:24

So in the previous slide we defined variables with little a, now we're

Â defining it with a big A, MATLAB will recognize these as two separate variables.

Â So, you need to keep that in mind and you need to keep track of what you

Â mean when you use lowercase, letters and what you

Â use, what you mean when you use uppercase letters.

Â And the second important point this illustrates has

Â to do with the semicolon here in the middle.

Â A will be defined like this: 1, 2, 3 on top of 4, 5 and 6.

Â So now we don't have all these, these six numbers all in a row.

Â We have first three numbers in the first row

Â and then next three numbers in the second row.

Â So what the semicolon here, indicates

Â is that we should have vertical concatenation.

Â And that means move to a new row.

Â So the comma like we showed before is for horizontal concatenation.

Â Put a bunch of numbers next to one another.

Â The semicolon is used for vertical, vertical concatenation, put a set of

Â numbers on top of another set of numbers, move to a new row.

Â 7:49

you can manipulate them and you can combine them with each other.

Â For instance, you could say, C equals A semicolon B.

Â That's going to take the array A, and vertically concatenate it with the

Â array B, so we'll end up with something that looks like this.

Â These six numbers here, this two by three array is what

Â we had A, and that's on top of this three by

Â three array, B, and they're all together in a five by

Â three array that we call C, that concatenates two matrices together.

Â Now what if we take instead, C equals A comma B?

Â In that case, we get an error and it would say this, error using horzcat.

Â CAT arguments dimensions are not consistent.

Â 8:29

What that means is that we've tried to horizontally concatenate a two by

Â three array, A, with a three by three array, B, and that's not

Â possible because A has two rows, and B has three rows, and therefore

Â they cannot be combined horizontally and so that's what this error here indicates.

Â It says you can't horizontally concatenate two things, two

Â objects, two variables that have different numbers of rows.

Â [BLANK_AUDIO]

Â Now we'll show some more examples of, of concatenation using MATLAB.

Â What if we typed a equals 1 colon 3, we have the numbers 1 through 3.

Â Then we could type a equals close bracket, square bracket, a comma, a.

Â We would get this answer here: 1, 2, 3 next to 1, 2, 3.

Â Now, for you know, this is not a formally mathematically correct, statement, right?

Â A cannot be equal to a and a, together.

Â This is the difference between

Â formal mathematics and, and programming languages.

Â What this is saying here in MATLAB is

Â you already have something called a, and you can

Â redefine a, you can define it to be

Â something new, based on the previous value of a.

Â So it is okay to have a on both the left-hand

Â side of the equation and the right-hand side of the equation.

Â These don't have to be exactly equal to one another.

Â The whole idea is that what you get on the left-hand side is what

Â you get new and the right hand side is something that you already have.

Â So variable a can appear on both the left and right hand side.

Â This is a way of defining the new value of a based on the old value of a.

Â 10:53

The analogy to use here is if you're, were editing a document using Microsoft Word

Â and you just called it document or you

Â just called it, mytermpaper.doc, or something like that.

Â Every time you, you went in and edited it, whatever you had before would be lost.

Â So if you typed up a paragraph that you thought was very

Â eloquent, you went back the next day and you edited it, and

Â you didn't like the new version, you said, I want to go back

Â to the old version, well you wouldn't be able to do that, right?

Â Because whatever you had before is going to be lost

Â if you keep saving it to the same file name.

Â Here, we keep saving this to the same value,

Â the same variable name, which we're calling little a.

Â 11:30

If you wanted to, keep your previous version

Â of your term paper you would call it

Â my term paper version one, my term paper

Â version two, my term paper version three, et cetera.

Â So analogously in MATLAB you could keep, redefining it into a new variable name.

Â If we had a equals 1, 2, 3 and then we said b equals a comma a then b would

Â be equal to this one by six vector here and

Â we'd still have our old value of, of little a.

Â So now the original definition of a is maintained, so as you're, as

Â you're programming in MATLAB, you need to think about this, you know, do

Â I need to keep the old version of this, or is it okay

Â to overwrite it and just throw away the old version of, of my variable?

Â 12:15

Now let's talk about some of the differences

Â between matrix arithmetic verses array arithmetic in MATLAB.

Â If you have a two by three or A, A with numbers

Â 1 through 6 arranged like this sd we've seen and then you have

Â a second two, two by three array called B with six different numbers, it's easy to

Â perform manipulations,uh, arithmetic manipulations on arrays A and B.

Â For instance you could say C equals two times A,

Â and that will just multiply two times each element of A.

Â You could say D equals A times three, sorry A plus three.

Â That will add three to each element of A.

Â And then you can combine these kinds of commands,

Â you can say E equals two times A plus B

Â and what you'll see here is you have 2 times

Â each element of A, plus the corresponding element of B.

Â For instance you have 2 times 1 is 2, plus 4 equals 6.

Â 2 times 2 is 4, plus 1 equals 5.

Â Alright so that multiplies each element in here by 2

Â and adds, to the, array of B element by element.

Â Now here things get a little more complicated.

Â What if you wanted to say, F equals A times B?

Â In this case you would get an error and the reason you

Â would get an error is because you're trying to do a matrix multiplication.

Â So that's what this error message means.

Â Error using mtimes or mtimes being the shorthand for matrix multiplication.

Â And the reason you get this error

Â is because the inner matrix dimensions must agree.

Â So A and B are each two by three arrays, and,

Â two by three matrices cannot

Â be, multiplied using formal matrix multiplication.

Â 13:50

And in, pretty soon we'll talk about the difference between array

Â multiplication and matrix and multiplication and

Â what matrix multiplication, MATLAB actually means.

Â Now you might not want to do a formal matrix

Â multiplication, in fact a lot of the times you don't.

Â What you might want to do instead is take 1 times 4, 2 times 1,

Â 3 times 7, you might want to multiply two arrays on an element by element basis.

Â And you can do this in MATLAB, but

Â you have to add something to your multiply command.

Â You have to add a dot.

Â If you say G equals A dot times

Â B then you're going to get this element by element

Â multiplication, 1 times 4 is four, 2 times 1 is 2, 3 times 7 is 21, et cetera.

Â So this is a very important point in MATLAB.

Â If you just do a straight multiplication, MATLAB

Â is going to interpret that as a matrix multiplication.

Â If you do a dot times matrix will, sorry MATLAB will

Â interpret that as a, an array, or an element by element multiplication.

Â 14:48

Now something else you can type is H equals A greater than B.

Â This is a, another type of command that is, not formally mathematically correct.

Â In formal mathematics it doesn't make any sense to say

Â is one array bigger or, or smaller than another array.

Â But the way MATLAB interprets this is on an element by element basis.

Â So what you get when you do this A is

Â greater than B is a bunch of ones and zeros.

Â Every time an element of A is not greater than the element of

Â B, for instance 1 is not greater than 4, you get a zero.

Â A zero meaning false, in this case.

Â But when an element of a is greater than the corresponding element of

Â e, for instance, 5 is greater than 2, then you get a 1 here.

Â 15:45

If you perform several computations in a row in

Â MATLAB it can be difficult to keep track of

Â all the variables you've defined and what the dimensions

Â of each variable are and what each variable represents.

Â In that case a command that's very helpful is what's known as the, the whos command.

Â This will list all of the currently defined variables.

Â For instance, if you've typed, a series of commands similar to

Â the ones we, we just showed you and you type whos

Â you may see these are the, the names of, these are

Â the, the names and these are the dimensions of all your variables.

Â And when you get an incompatible, incompatibility error, it can be useful

Â to type whos to figure out why you got an incompatibility error.

Â For instance you could say, well, A, capital A in this

Â case is two by three and little a is two by seven.

Â So of course I am not going to be able to multiply those two.

Â 17:38

We could also say little f is c five comma three.

Â That would be fifth row, 1, 2, 3, 4, 5.

Â Third column, 1, 2, 3.

Â And so therefore we would get f equals 9.

Â Either one of these will access a single element of the array.

Â If we wanted to access more than a single element.

Â If we wanted to access a row or a column, we can type this.

Â D equals C colon comma 1, so it's like what we did

Â here with 1 comma 1, except we replaced 1 with a colon.

Â What do we get in this case?

Â We get a 5 by 1 vector, 1, 4, 1, 4,

Â 7, which we can see is the very first column of C.

Â So what, we, what this colon does is it says, access

Â all the rows and then this 1 says access the first column.

Â So that's a way to access an entire, entire column of an array.

Â And we can make this more complicated.

Â We can say E is C colon 1 comma 3, which will give us a five by two

Â array, which has the first column and then the third column of C.

Â 19:16

Or we could say something like C 2 comma 5 and comma, 1 colon 2.

Â So this is take the second row and the fifth row, and then take

Â the first column up to the second column and this is what we get.

Â 4, 7 is the first column, 5, 8 is the second column.

Â Finally, if we said capital H equals C, 1 comma 5,

Â 1 comma 5 in this case we would get an error.

Â The reason we would get an error is because 1 through 5 is

Â fine for, specifying the rows of C, but how many columns does C have?

Â C only has three columns.

Â So, it doesn't make sense to say the first column all the way up to the fifth column.

Â There is no fourth column and there is no fifth column.

Â And that's when you're going to, why you're going to get an

Â error and the error will say index exceeds matrix dimensions.

Â [BLANK_AUDIO]

Â Now let's go back to the, that error that we saw

Â to do before, when we tried to do a matrix multiplication.

Â Remember when we had A as a two by three array and B as a

Â two by three array, and then we said capital F is equal to A times B.

Â We got this error here.

Â Error using mtimes.

Â Inner matrix dimensions must agree.

Â Let's, now let's understand the basis for this error.

Â [NOISE]

Â [BLANK_AUDIO]

Â Now we can understand how matrix multiplication

Â works mathematically and how it works in MATLAB.

Â Again if we have A with, as a two by three array, and B as a two by

Â three array, then we can not multiply a two

Â by three array, by a two by three array.

Â So since A and B are both two by three in this case, A times B is undefined.

Â We'll get an error is we try to do that.

Â But what if we took b and converted it to a three by two array.

Â One way we could do that is by using a command known as transpose.

Â When we type B apostrophe here, B apostrophe means

Â take the transpose of B, which in practical terms means

Â turn a row into a column, turn each row

Â into a column and turn each column into a row.

Â So B apostrophe or B transpose would look like this.

Â You see 4, 1, 7, which is the first row of B becomes the first column of B transpose.

Â And 9, 2, 3 becomes the second column of B transpose.

Â So, in this, now in this case, A is two by three, and B transposes three by two.

Â And remember that for, matrix multiplication to

Â occur, the inner dimensions, matrix dimensions, must agree.

Â So you can take a two by three

Â and multiply it by something that's three by two.

Â So you can compute A times B transpose.

Â And if we do do that computation in MATLAB, F equals A

Â times B transpose, we don't get an arrow we get an answer.

Â And we get this answer, F equals this two by two array, 27, 22, 63, 64.

Â We can understand each one of these elements as follows.

Â 27 in this case is 1 times 4 plus 2 times 1 plus 3 times 7.

Â So you take this first row of A, 1, 2, 3 and

Â you multiply it by this first column of B transpose 4, 1, 7.

Â And you sum up all the products.

Â So it's 1 times 4.

Â 2 times 1.

Â 3 times 7, right?

Â 1 time 4 is 4.

Â 2 times 1 is 2.

Â 4 plus 2 is 6.

Â 3 times 7 is 21.

Â 6 plus 21 equals 27.

Â That's why we got a 27 for the first row and first column, of F in this case.

Â And all the other elements of, of F are computed similarly.

Â [BLANK_AUDIO]

Â So we can understand matrix multiplication a little bit more

Â generally by, specifying a couple of rules that govern matrix multiplication.

Â One, as we said, is that multiplication

Â can only occur if the inner dimensions agree.

Â And furthermore, what's the, what are the dimensions of our product going to be?

Â If A is an n by m matrix, and B is an m by p

Â matrix, then the product A times B is going to have dimensions n times p.

Â So, the n and the m have to match.

Â And then what you have, what you start with is

Â the number of rows you start with in the first matrix.

Â And the number of columns you end with in the second

Â matrix, are what you're going to get in your product, n times p.

Â So we can visualize this as follows.

Â You can take something that's tall, and skinny,

Â multiplying it, by something that's short and pretty

Â fat, and you can end up with a product that's short, sorry that's tall and fat.

Â 24:08

So if you started with something that was

Â twenty by fifty, multiplied it by something that

Â was fifty by eight, you would end up with a product that was twenty by eight.

Â So this is, these are the rules for a matrix multiplication.

Â And these are some examples of the things that

Â you can get when you do real matrix multiplication.

Â This is true using MATLAB and then, this is just

Â true in general, in terms of the rules of mathematics.

Â [BLANK_AUDIO]

Â So in summary, what we've seen in this first lecture are that

Â variables in MATLAB can be either scalars, vectors, or they can be arrays.

Â And array dimensions influence the types of operations that can be allowed.

Â For instance, arrays can only be added,

Â or multiplied, or concatenated if the dimensions match.

Â And we saw some examples of where these

Â dimensions didn't match and where MATLAB gives you errors.

Â 24:57

And then the other key point that we're going to come

Â back to we, we saw that for the first time in this

Â lectures, in this lecture, is that certain symbols, such as the

Â colon, the semi colon and the period have special meaning in MATLAB.

Â And we'll come back to those, those meanings later.

Â [BLANK_AUDIO]

Â [NOISE] So now that we've finished this lecture,

Â let's move onto a couple of self-assessment questions.

Â This is a good way for you to, to, you know, take a look at the questions,

Â see if you can answer it and check

Â to see how well you're understanding, what we've covered.

Â So, you can either do these questions now, or, if

Â you feel like this was, some of this was kind of confusing,

Â maybe you want to watch the lecture a second time and take

Â the self-assessment question after watching the lecture for a second time.

Â So, the first question we've devised for you is as follows.

Â In analyzing data, you have to, to find an array called capital A.

Â This array has 32 rows and 15 columns.

Â Now you want to type a command to access a subset of A and this is what you type.

Â Capital B equals A 20 colon end comma and then square

Â bracket 1, 3, 6, 11 end square bracket, end parenthesis, semicolon.

Â