0:00

[MUSIC]

Now that we know how to make a matrix, we need to know how to access parts of it.

So, for example, if we want to see just the one element on the second

row in the third column, we'll know how to do it.

0:29

And here it is.

As you can see, its dimensions are 3 by 4, meaning that it has 3 rows and 4 columns.

And they're numbered from top to bottom.

1, 2, 3.

And the columns are numbered, too.

1, 2, 3, 4, from left to right.

So how do you look at just the element on the second row and the third column?

1:13

As always, the row comes before the column.

Together, the two indices, 2 and 3, in parentheses and

separated by commas, tell MATLAB which element you want to see.

1:34

Okay, well, let's look at this same example in MATLAB.

We'll set X equal to left bracket, 1 colon

4, semi-colon, 5 colon 8,

semi-colon, 9 colon 12, right bracket.

Note how we've used the colon operator to save typing.

You know, we could have done it the hard way, which would have

been X equals 1, 2, 3, 4, 5, 6, you know, et cetera.

I'm not even going to do that.

It's just too much work.

But the colon operator works perfectly.

Each row of a matrix is simply a vector of numbers.

And the colon operator produces a row vector of evenly spaced numbers.

So if we want the numbers on a row to be evenly spaced,

we can use a colon operator instead of typing them out.

And that's what we wanted this time, so we used the colon operator.

Okay, to continue with our example,

let's specify the third element on the second row, as we did before.

So, we say x, parenthesis now,

2, 3, right parenthesis.

And we see the answer is 7.

We always, as here we did, give the row index first and the column index second.

3:27

on the third row——and here's the 3 down here——

in the third column—— here's the second 3——we find the 11.

And we can assign a new value to a specified element.

X(2,3), instead of being 7, I'd like it to be 97.

So we set it to 97.

You'll notice that while we only mentioned to MATLAB one element,

3:55

Well, we will notice, however, that it did make the change we requested.

So element 2, 3, that is second row, third column, is now 97.

And if we wanted the second row, second column to be——

well, I don't know——a hundred and twenty-three.

If we look at it now,

4:19

it's a hundred and twenty-three.

So what happens if we assign a value to an element of a matrix, but

the matrix doesn't exist?

Let's say, for example, the matrix XYZ.

If we look over in the Workspace, we'll see there's no XYZ matrix.

So it doesn't exist yet.

4:45

Hm, we get one of those awful red messages saying we made some kind of error.

Okay, MATLAB, I did this on purpose.

It's not really an error this time.

But anyway, let's set a value to it.

XYZ, element

2,2 is equal to one hundred twenty-three.

So we're doing the same thing we did with X, but

now we're doing it with a non-existent matrix.

So you expect more red to come back, I'm sure.

Well, surprise!

What is this?

XYZ equals 0, 0, 0, 123.

Well, here's what MATLAB did.

It said: You want to have a matrix XYZ, and you want to have 123 on row 2, column 2.

I'll give that to you.

You didn't tell me what goes on the rest of the rows and columns, so

I'll put zeros there.

And that's what MATLAB does.

It makes the smallest matrix it can and still accommodate your request.

In other words, the smallest matrix it can that has an element, 2, 2.

And that's a 2 by 2 matrix.

6:22

Let's assign a value to element 4,5,

say 456.

So what's this?

Well, MATLAB has to extend the size of the matrix from 3 by 4 to 4 by

5 to accommodate our request to put something at element 4, 5: the 456.

There it is.

It's right there.

We didn't tell it what else to put on the fourth row and

we didn't tell it what else to put on the fifth column, so it put zeros there.

6:55

There's one pitfall we need to point out

concerning the first element on the first row.

Let's set that first element on the first row to the value 99.

It's 1 right now, and we'll change it to 99.

There.

So we see our matrix with the 99 replacing the 1.

7:20

X as a scalar is the same as a 1 by 1 matrix.

And it kind of looks like the same meaning here, but it's not.

It's entirely different.

What we have just done is obliterated our entire matrix and

replaced it with just the 1 by 1 scalar matrix, 99.

Well, we know how to access or change individual matrix elements.

But, you know,

we can change multiple elements at a time using the so-called subarray operations.

Let's see how that works.

Let's come up with a little simpler matrix this time, just two rows.

It’s one of my favorite matrices because it's so easy to type.

One, two, three, four, five, six.

Let's look at a sub-array.

I'm going to type X two comma and one three.

What I've done here is I said that I'm interested in just the second row and

I'm interested in the first and the third columns.

I want to see that.

So, MATLAB slices off that second row, and it gives me the first and

the third element on the row, and there they are: four, six.

This is a sub-array. This is the whole array, and

this four and this six taking together is the subarray that we’ve specified.

The comma's required, by the way: this one I'm talking about.

Let's try doing without it.

[CLICKING] We get an error. And it's just as

easy to specify multiple rows as multiple columns.

So here we go.

I'm going to say I want row two first and then row one, and

I just want to look at the column two.

9:22

We've asked for row two first—and there's the five—and

then row one—and there's the two.

We asked for second column in each case.

So it puts the five first and then the two.

And here's a combination of multiple rows and multiple columns.

9:39

From our rows, I'm going to say I want row two first, and then row one, and

then row two, and then for the columns,

I want to start with three and go to one and one again and then two.

You can do anything you want to

as long as you're picking elements that are actually inside the array.

10:01

So let's just look briefly here.

We took row two first, and

row two had a four, a five, and a six on it.

And we asked for columns three and then one twice and then two.

So here the six, the four, the four is repeated because we asked for

it twice, and then the 5, and then you can see what happened with the other rows.

You can do any combination you want as long the elements you request exist in X.

Let me give an up-arrow to repeat the previous command.

I'm going to change that two to an eight and hit Enter.

11:19

This not really a new concept.

It's just a combination of two concepts that you’ve already learned.

First, you learned that a vector like 1, 2,

3 can be used in a subarray operation. And second,

you learn that the colon operator can produce a vector of evenly spaced numbers—

in this case, one, two, three. [CLICK]

And you can get as fancy as you want with this.

For example, let's do this: We'll put two,

colon, minus one, colon, one, for the rows we want and for

the columns, three, colon, minus one, colon, one.

We're going backwards on both of them. And there you have it.

12:05

MATLAB provides a very helpful way for

specifying the last index of a row or a column.

Here's an example.

[CLICKING] As you might guess, the word “end” here

means the last row index in the matrix X.

And here's how you ask for the last column index. I'm sure you can guess.

13:15

The keyword “end” may not seem all that helpful to you right now, but

we'll see later that it's very helpful when we write a program in which we can't

know what the last index is.

That actually happens a lot.

When it does, “end” will always give it to us.

13:30

Here's some more examples of end.

[CLICKING]

you can do it in

both positions.

[CLICKING] And here's

two, end, one

end three.

So we pick the second last, the first, and

then the last element again on the third column.

And you might be surprised to find that “end” can be used in arithmetic expression.

14:24

Subtraction's the most common.

Let's do one of those.

Let's remind ourselves what X is, now.

It's one, two, three, four, five, six. So here we go: one, end minus one.

So this means we want to be on the first row, and

instead of the last element, we want one before the last element.

end minus one means end, which is the index, three, minus one,

which gives us the one before that too.

And here's another one [CLICKING].

And finally, if you want the last two

elements of the first row

[CLICKING] in reverse order,

you get them.

Addition is a little bit less common with end, and it'll cause an error if

you're using it to look beyond the end of a matrix or trying to, like say this:

15:38

But it's perfectly legal in an assignment operation,

like say this.

[CLICK]

Notice that X has gotten bigger.

Up here, we see it was one, two, three, four, five, six, two rows, three columns.

Now it's 1, 2, 3, 4, 5, 6, 17, 0, 0.

When we put end plus 1, we said we wanted to put 17 on the third row.

Well there wasn't a third row, so MATLAB added one.

We told it what to put in the first column.

We didn't tell it what to put in the other two columns.

So as usual it guesses that we want zeroes there.

16:39

Let's see what we've done here.

We want row one here, and we want to start at column one,

and jump by two until we get to the end.

Well, row one has a 1, 2, and a 3.

We jump over that two and we get a 3, 1, 3.

16:58

Here we want the third row, and here's the third row.

And we want to start at the end and go backwards to 2, 0, 0 and there's the 0, 0.

And in this last one, we want row 1—here's row 1.

And we want to start, not at the end, but at one short of the end. That's here.

And go to the end 2, 3.

And so you see we have a 2, 3 here.

17:23

Well, MATLAB also provides a shorthand for one particular phrase:

1 colon end.

Let's start, first of all, by putting 1 colon n 2.

2, 5, 0. We want to go from the first row to the last row, and

we want to stick on column two, and here it is.

18:27

And of course this is even longer.

[CLICKING] But all three of these things give us the same thing.

But the point here is that MATLAB opts for consistency, which means it

always lets you use these expressions for any index with no exceptions.

This emphasis on consistency is a hallmark of a good programming language,

and all the programming languages that are in heavy use today, including C,

C++, and Java, and even good old Fortran, all emphasize it too.

19:01

Well, so far,

we've used the array operations just to look at elements inside a matrix.

But, you can also use them to change the values of elements inside a matrix.

Let's look at an example of that.

I'm going to give the subarray one to end and one.

And if we look at that, we see that's elements 1,

4, and 17 there on the first column.

20:04

Now, what we've done is told MATLAB that we're interested in all the rows and

we're interested in columns two and three.

And we want to set all those elements, each to the value of 9.

Arrays can be on the right side too.

Let me show what I'm talking about with an example.