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.