0:31

We had a surprise then.

You may remember it.

The transposition operator, which was written after the colon operator,

was actually applied before the colon operator.

It just transposed the 5, which did nothing.

0:44

Transposition went first, because it ranks higher than the colon operator.

To make the colon go first, we had to add parenthesis, like this.

Those parenthesis force the colon operator to make the row vector first, so

the transposition operator could then make it into a column vector.

Well let's look at a more familiar example, one involving arithmetic.

1:09

Here the multiplication operator is written after the addition operator, but

it's applied before the addition operator.

So 3 is multiplied times 4 first to get 12.

And then 2 is added to get 14.

It's not the left to right order shown on the right,

which would result in 5 times 4 equals 20.

Here again, the non-left to right order happens because the operator on the right

outranks the one on the left.

This time it's multiplication,

which is ranked higher than addition, and it goes first.

In programming, we call this ranking “precedence” and

we say that multiplication has a higher precedence than addition.

2:17

Next we have exponentiation and

transposition, which both have a precedence of 1.

Before we look further, it's

important to note that lower numbers actually mean higher precedence.

2:43

Down at the very bottom is the poor old colon operator,

which explains why it came after the transpose.

At the number 2 position, you may be wondering what “unary plus” and

“unary minus” are.

Well “unary” means that the operation involves only one operand.

3:18

But the more common operators are the binary operators,

which means that they take two operands, and here you see some examples.

3 minus 4, X dot star Y, which, you will remember, is array multiplication.

X star Y, which is matrix multiplication, and

A to the 3rd power, which is exponentiation.

4:01

Associativity is simpler than precedence, and

we'll start with a very simple example:

6 plus 2 plus 3.

A computer can carry out only one operation at a time, so

it adds the 6 to the 2 to get 8, and then it adds the 3 to 8 to get 11.

The order is left to right, simple.

But what about this, 6 minus 2 plus 3.

Which goes first?

The minus or the plus?

We need to know because the results will be different, 7 or 1.

Well, maybe the precedence table will give us the answer.

4:47

In programming, the order in which operators of the same

precedents are executed is called “associativity”.

In MATLAB, it is always left-to-right.

So we say that MATLAB uses left-to-right associativity.

As a matter of fact, so most all other languages do.

So this expression evaluates to 7.

Here are a couple of other examples.

6 divided by 2 equals 3. 3 times 3 gives 9.

Both of these operators have a precedence of 3.

In the last expression, 6 to the 2 power is 36,

and 36 to the 3 power, well, we'll have to ask MATLAB.

Whatever it is, I'm sure it's a pretty big number, so let's do that, in fact.

Let's ask MATLAB to evaluate some expressions that require it to use

these rules.

Okay, let's start with the first example we did to illustrate precedence.

2 plus 3 times 4.

As promised, multiplication has the higher precedence,

so the actual order is like this.

[CLICKING] The redundant parentheses

show that 3 times 4 goes first.

That gives 12, and we add 2 to 12 and get 14.

And if we force addition to go first with parentheses,

[CLICKING] we get a different result.

2 plus 3 equals 5, and 5 times 4 gives 20.

As I've mentioned before, we're used to carrying out multiplication before

addition, even when addition is written first in the expression.

6:46

Well, remember that the plural colon operator has a lower precedence than all

of the arithmetic operators.

To be specific, the colon is at 5 while addition and subtraction are at 4.

And smaller numbers go first.

So, this is what we did.

[CLICKING] The 3 plus 10 went first,

so we're given the colon operator 1 colon 13.

If you want to add the 10 after the colon operator has made its row vector,

you need to force it with parentheses like this.

[CLICKING] There, we got 1, 2, 3, and

then we added 10 to each element,

and got 11, 12, 13.

And by the way, note that we're adding a scaler to a row vector.

So this is an example of a scalar being added to a matrix.

In this case, the matrix has just one row.

7:51

If you don't know which operator goes first, you have three options.

One, use parenthesis to force the order.

Two, try an example and learn the order.

Or three, look at MATLAB's precedence table and read the order.

8:11

Well, it's right at your fingertips.

Just type help precedence.

[CLICKING] And there's MATLAB's precedence table.

You'll note that there's no parenthesis here.

9:00

The actual order is like this,

6 plus 2 in parenthesis plus 3.

Because as we've seen, MATLAB always evaluates from

left to right when the operators have the same precedence.

And of course, since we have two pluses, they have to have the same precedence.

9:22

But you can't tell with addition,

if we force right to left order with parenthesis like this,

[CLICKING] we get the same result, as you knew we would.

When the right-to-left order gives the same result as the left-to-right order,

the operator is said to be “associative” in both mathematics and computer science.

So addition is associative.

[CLICKING].

10:18

because at the risk of being boring, or

perhaps I should say even more boring, MATLAB always evaluates

from left to right when the operators have the same precedence.

If we force right-to-left order with parentheses like this,

[CLICKING].

10:43

the result is different.

So subtraction is not an associative operation.

And when we mix subtraction and addition, which have the same precedence, like this,

[CLICKING] since they have the same precedence,

the order is, well, you know.

I just can't bear to say it again.

Now let's look at multiplication and division

with this example, which we've seen before.

[CLICKING] And let's use a pair of redundant parenthesis to make the order explicit.

[CLICKING] This one sometimes surprises people because

it might look like multiplication should go first.

It doesn't.

11:59

And finally, I sense that you are burning with curiosity to find out what 6 to

the 2 to the 3 equals.

Well, as promised, I'm going to ask MATLAB right now.

6 to the 2 to the 3.

[CLICKING] Since the left hand carat goes first, this is 36 to the 3rd power.

And as I guessed before, it's a pretty big number.

46,656.

But just out of curiosity, what would the answer be if the 2 were

raised to the 3 power first and then 6 were raised to that power?

Well, we'll have to use parentheses to find out.

6 to the 2 to the 3 power.

[CLICKING] The result is different, which shows that exponentiation

is not associative, and the result is a really big number.

Let's say, that's 1,679,616.

Kind of amazing you can get such a big number with just a 6, a 2, and a 3.