[MUSIC]
To see how the colon operator works let's start with a simple example.
x = 1:3:7.
Hit return.
And I get one, four, seven.
x is a vector.
Here's what the meaning of this colon operator is.
And notice we’ve got two colons in this so-called “colon” operator.
It means start at one, increase by steps of three, and
stop, before you go over seven.
Go no higher than 7.
So we add 3 plus 1.
And we get 4.
We add 3 to that.
We get 7.
If we added 3 to that, well it would be over 7.
This concept of incrementing and
going no higher than a limit, sometimes is confusing.
Let's look at 1,
3, 8.
We start with 1, we add 3, and we continue, but we go no higher than 8.
Well we got the same result here,
because if we added 3 to 7, we would be higher than 8, so it stops at 7.
Let's try this one.
Still no joy.
Let's try this one.
We want to get another number.
Nothing's happening because, if we added 3 to 7, we'd be higher than 9.9.
So finally let's do this, 1, 3, 10.
This starts at 1, we add 3 and get 4, we add 3
and get 7.
We add three and get ten.
MATLAB checks.
If you add three to ten would you be higher than ten?
Well of course, so it stops.
There's a special shorthand if you want to go up by one instead of, say, three.
Let's call this variable ints. We'll set it to one to one hundred.
This is the same as going up by one.
Just one colon is needed.
And, well, we'll get, a hundred numbers here.
Let's look at the size of this variable.
[CLICKING] It's a 1 by 100 vector as you can probably see.
So we call this the “colon operator”. Well, what's the definition of an operator?
Pretty simple.
You've seen a function.
An operator is a function that's invoked by a symbol.
A function is an operation that's invoked by the name,
so the name of this special kind of function
is a single symbol, or 2 symbols as in the case of 2 colons here.
Plus and minus are probably the most familiar examples.
Let's look at 1 plus 2. Of course we get 3.
There's another function
that does the same thing in MATLAB: plus 1,
2. The first one is an operator. The second one is a function.
They're both called “plus”. One's a word. One's a symbol.
There's a colon function
that does the same thing as [CLICKING] this,
so the first one is the name “colon”.
The second one is the colon symbol.
As you learned in lesson 1, we call the inputs to a function call “arguments”.
For an operator, we call them
“operands”.
For a function the arguments are surrounded by parentheses and
separated by commas.
For operators the inputs are separated by the operators themselves.
For example 1 plus 2.
Plus separates the two operands, 1 and 2.
If you had multiplication, 10 times 20, the operands are 10 and 20.
They're separated by the operator: asterisk.
And of course here we have, 1 and 7 separated by the colon.
And you can even have two symbols in the colon operator.
So we can separate three operands with the two colons.
The colon operator's very useful for
generating huge vectors of equally spaced numbers.
Suppose we wanted all the odd numbers smaller than 1,000 starting with 1.
Might set z = 1 colon 2 colon 1000.
Before I hit return and fill the screen, notice that we're going to start at 1.
Then we're going to add 2. That'll give us 3.
Then we add 2 to that. That'll give 5.
1, 3, 5, 7, and so on, all the way up to, but as you know,
since 1,000 is not reachable as an odd number, it doesn't quite include 1,000.
So we go to 999.
If we added 2 to 999, we'd get 1,001 which would be greater than 1,000,
and MATLAB won't do that.
Let's look at the size of z.
It's one by 500.
There are 500 odd numbers from one to 999.
Suppose we wanted the even numbers that are smaller than or equal to 500.
Let's call those evens.
And we'll set them to 2 colon 2
colon 500.
We start with two, we keep adding two until we get to 500.
Two, four, six, eight, and there are those numbers.
What's the size of that vector?
[CLICKING] Well it's one by 250.
You can have decreasing
sequences too.
Let's start with 7, go down by 3, and end up at 1.
Seven, four, one.
So we start with seven, we subtract three, that gives us four.
We subtract three again, that gives us one.
If we subtract the three again, we'd be less than one, so the rule now is:
Instead of, “Go no higher,”
it’s, “Go no lower.” So we go no lower than one.
Let's look at one we call 'down_by_ten’.
It’s equal to 100, colon, minus ten, colon,
minus 100. So here we start at a hundred.
We decrease by 10—— we add minus 10 in other words——
until we get to, and don't go below, minus one hundred.
And there's the numbers. Size of this
[CLICKING] is
one by 21. Again, it's a vector.
You always get row vectors with a colon operator. You never get a column vector.
If you want a column vector you have to change it into a column vector,
and we’ll show you how to do that later.
I want to show you a strange thing.
x equals seven colon thee colon one. So innocently I type:
seven colon three colon one, meaning I want to start with seven, I want to change by
three until I get down to one, and I hit return, and I gets this strange thing.
x is this empty matrix.
One by zero.
Well what's the difference, last time I typed seven——
Well here I had a minus three; here I have a plus three——
and there's the problem.
But I want all the numbers that start at 7, get bigger by 3, and end up at 1.
Well there is no such number or numbers.
So it gives us the empty matrix.
Let's see what the size, of x is?
1 by 0, just as it said.
There are other sizes of empty matrices
in MATLAB.
And by “empty matrix” we don't mean a matrix with zeroes in it.
We need a matrix with nothing in it.
Here's how to get that other kind.
[CLICKING] You just type nothing! So we typed x, a left bracket, typed nothing,
and then typed a right bracket, and it shows that it's an empty matrix.
It shows it a little differently here. Let’s look at the size of x.
This time x is zero by zero. Before, it had one row with nothing on it.
You always get a row matrix, as I mentioned, with a colon operator, and,
sure enough, we got a row matrix here with nothing in it:
one row, no elements.
Here we didn't indicate whether we wanted one row of nothing or
one column of nothing.
So it gives us no rows, no elements.
[MUSIC]