I



I.4 MATLAB OPERATORS

I.4.1 Operators

Operators are the basis for building expressions. Operations include basic functions such as scalar addition and subtraction to more complex functions such as matrix multiplication and division. A comprehensive list of operators is provided in the first chapter.

Remember

In MATLAB there are three different groups of operators:

Arithmetic, Relational, and Logical

Remember

In MATLAB the order of precedence for the operators is

Arithmetic->relational-> logical

The first group of operators is the arithmetic operators. They include the basic math functions to perform addition, subtraction, etc. The syntax used in MATLAB for the basic arithmetic operators is as follows:

+ addition

- subtraction

* multiplication

/ right division

\ left division

^ power

The user should be familiar with the addition, subtraction, and multiplication operations. The right and left division, however, does need some explaining. The basic concept behind left and right division is as follows:

A/B = B\A

Right Division Left Division

In right division, the first number is the numerator and the second number is the denominator. Left division works opposite to right division; the first number is the denominator and the second number is the numerator.

Example I.4.1.1

Divide 2 by 4 using right and left division.

» 2/4

ans =

0.5000

» 4\2

ans =

0.5000

Before finishing this section, let’s do one more example with arithmetic operators.

Example I.4.1.2

In one command statement using the arithmetic operators, subtract 8 from 16 squared.

» 16^2 - 8

ans =

248

»

This example demonstrates that the power operator takes precedence over subtraction. Precedence of the power operator over subtraction means that the power operation is performed before the subtraction, regardless of the order of the two operations. Rules for precedence of arithmetic operators are as follows. Multiplication, right and left division and the power operator all have equal precedence. Combinations of these four operators in one statement are performed in the order that they appear from left to right. These four operators all have precedence over addition and subtraction. You can use parentheses to assert your own precedence in an arithmetic expression. We will see more examples of precedence when we discuss Relational and logical operators.

I.4.2 Arithmetic Array and Matrix Operators

The arithmetic operators mentioned in the previous section are not limited to scalar operations only; they can be used for arrays and matrices.

Remember

In MATLAB array arithmetic operations are carried out element by element

A list of the operators, their function, and their syntax is given below:

A + B addition of arrays A and B

A - B subtraction of arrays A and B

A .* B element by element multiplication

A ./ B right division of arrays A and B

A .\ B left division of arrays A and B

A .^ B Array A raised to the power of B

A . ‘ Transpose of array A

Remember

In using array operations, A and B either have to be of the same dimension or one of them has to be a scalar

The array operations are similar to the arithmetic operations mentioned in the previous section. The ‘.’ indicates the element by element operation. The next couple of examples show the use of some of these operators.

Example I.4.2.1

Define two arrays x = [5 6 7] and y = [7 8 9]

» x = [5 6 7]

x =

5 6 7

» y = [7 8 9]

y =

7 8 9

»

Example I.4.2.2

Transpose x and y

» x.'

ans =

5

6

7

» y.'

ans =

7

8

9

»

Example I.4.2.3

Multiply x and y. Then multiply x by the transpose of y

» x.*y

ans =

35 48 63

» x.*y'

??? Error using ==> .*

Matrix dimensions must agree.

»

Again remember the dimensions must agree!! In the previous example, the error was shown because we tried to multiply a 1 by 3 array with a 3 by 1 array.

Example I.4.2.4

Divide x and y using left and right division.

» x./y

ans =

0.7143 0.7500 0.7778

» y.\x

ans =

0.7143 0.7500 0.7778

»

Example I.4.2.5

Raise x to the power of 2. Then raise x to the power of y.

» x.^2

ans =

25 36 49

» x.^y

ans =

78125 1679616 40353607

»

In example I.4.2.5 above, we raise x to the power of y in an element-by-element fashion by using the ‘.*’ operator. The array x contains the elements 5, 6, and 7 and the array y contains the elements 7, 8, 9. So the element-by-element power operation raises 5 to the power of 7, 6 is to the power of 8, and 7 to the power of 9.

As you can see, the use of array operators is not that difficult. The information learned here will be built upon in the next section when we talk about matrix operators.

I.4.3 Arithmetic Matrix Operators

Matrix arithmetic operations are defined by the rules of linear algebra. The list of operators is similar to the list of operators used for array operations. The list of operators and their functions are listed below:

A + B Adds A and B

A - B Subtracts A and B

A * B Algebraic Multiplication of A and B

A / B Matrix right division

A \ B Matrix left division

A ^ p Matrix power where p is a scalar or integer

A’ Matrix transpose (for complex matrices performs

complex conjugate transpose)

Matrix operators are very useful and frequently used for solving linear equations. Some examples are shown here to familiarize the reader with Matrix operators.

Remember

The order of operations is important when using Matrix operations

Example I.4.3.1

Create matrices P and Q. Let P be [1 4;4 5] and Q be [6 5;4 5].

Multiply P and Q, then Q and P, and array multiply P and Q, then Q and P.

» P = [1 4;4 5]

P =

1 4

4 5

» Q = [6 5;4 5]

Q =

6 5

4 5

» P * Q

ans =

22 25

44 45

» Q * P

ans =

26 49

24 41

» P .* Q

ans =

6 20

16 25

» Q .* P

ans =

6 20

16 25

»

As mentioned previously, the order of matrix operations is important. P * Q does not equal Q * P. Note that this restriction is not necessary for array multiplication. P. * Q is just an element-by-element multiplication of the matrices, therefore P .* Q equals Q.* P.

Example I.4.3.2

Perform matrix right and left division on matrices P and Q.

» P/Q

ans =

-1.1000 1.9000

0 1.0000

» Q\P

ans =

-1.5000 -0.5000

2.0000 1.4000

»

Again, the order of the operations is important. It is interesting to note that right division of matrices is equivalent to multiplication of the first matrix times the inverse of the second matrix. That is, A/B is equivalent to A*inv(B). Similarly, for left division, A\B is equivalent to inv(A)*B.

Example I.4.3.3

Raise P to the second power. Again use matrix and array operators to see the difference.

» P^2

ans =

17 24

24 41

» P .^2

ans =

1 16

16 25

»

Note that P^2 is equivalent to matrix multiplication of P times P.

At this point you may be asking yourself, “How do I know when to use array and matrix operators?” If you want to perform element-by-element operations, use array operators. Suppose, for example, you are solving for X in the equation AX = B where A and B are both matrices. Since this is a matrix equation, the solution will involve matrix operations. The solution for X is of the form X = inv(A)*B. The abbreviated solution for this expression is X= A\B. The preceding is an instance where matrix operations are appropriate. The following is an example where element-by-element operations are appropriate.

Example I.4.3.4

Suppose you would like to calculate power dissipation in a diode given the following measurements of diode voltage and current:

|Diode Voltage |Diode Current |

|0.5 volts |10 (A |

|0.6 volts |1mA |

|0.65 volts |4 mA |

|0.7 volts |10 mA |

|0.75 volts |40 mA |

|0.8 volts |100 mA |

We know that power is voltage times current. In MATLAB, we can define a voltage array, v, and a current array, i, and use element-by-element multiplication to calculate the power.

» v= [0.5 0.6 0.65 0.7 0.75 0.8] % voltage array

v =

0.5000 0.6000 0.6500 0.7000 0.7500 0.8000

» i = [10e-6 1e-3 4e-3 10e-3 40e-3 100e-3] % diode current in amps

i =

0.0000 0.0010 0.0040 0.0100 0.0400 0.1000

» p=v.*i % diode power in watts

p =

0.0000 0.0006 0.0026 0.0070 0.0300 0.0800

» format short e

» p

p =

5.0000e-006 6.0000e-004 2.6000e-003 7.0000e-003 3.0000e-002 8.0000e-002

»

It takes some practice to remember what the syntax for a given operation should be, but once you have used these operations for a while, the appropriate usage should become second nature.

I.4.4 Relational Operators

Relational operators perform element-by-element comparisons between two matrices or scalars.

Remember

MATLAB will return a 1 if the relation is true and a 0 if the relation is false

The Relational operators, their syntax, and their functions are listed below:

A < B checks if A is less than B (works only on real part)

A > B checks if A is greater than B(works only on real part)

A = B checks if A is greater than or equal B(works only on real part)

A = = B checks if A is equal to B(works on real and imaginary part of operands)

A ~= B checks if A is not equal to B(works on real and imaginary part)

All the relational operators are relatively self-explanatory. To understand relational operators and their uses, some more examples are presented.

Example I.4.4.1

Create variables a and b. Set a to 6 and b to 8. Then use some of the relational operators to compare a and b and observe the results.

» a = 6

a =

6

» b = 8

b =

8

» a > b

ans =

0

» a < b

ans =

1

» a == b

ans =

0

»

Note that relational operations return a "1" if the relationship is true, and a "0" if it is false.

Example I.4.4.2

Create matrices c and d. Let c be a 2x2 matrix equal to [1 5;7 3] and d be a 2x2 matrix equal to [9 5;2 7]. Again use some of the relational operators to compare c and d and see what type of results you get.

» c = [1 5;7 3]

c =

1 5

7 3

» d = [9 5;2 7]

d =

9 5

2 7

» c >d

ans =

0 0

1 0

» c >=d

ans =

0 1

1 0

» c == d

ans =

0 1

0 0

» c =f

ans =

1

»

As can be seen by the previous example, all the relational operators, excluding the (= =) and (~=) operators, test only the real part of the operands. Relational operators are very useful for comparing variables and for decision making, as required in for and if loops.

I.4.5 Logical Operators

The final set of operators is Logical operators. They perform the AND, OR, and NOT operations. The syntax used is as follows:

A&B A AND B

A | B A OR B

~A NOT A

xor(A,B) EXCLUSIVE OR of A and B

As is the case with Relational operations, if a Logical operation is TRUE then a 1 is returned and if it is FALSE a 0 is returned.

Remember

When Logical operators are used in calculations, they

have the lowest precedence

Some examples are presented here to understand Logical operators better.

Example I.4.5.1

In MATLAB write a command statement to test if 2+4 is less than 9 and 11.

» 2+4 < 9 & 11

ans =

1

»

There are some important things that should be noted in the previous example. First when looking at the command statement above, it should read: “Is the value 2+4 less than 9 and 11.” The answer is obviously true; however, it is important to remember that MATLAB used the order of precedence when performing the comparison. The addition of 2+4 was performed first since it has higher precedence than the AND operator. The sum was checked to see if it was less than both 9 and 11.

Example I.4.5.2

In MATLAB, write a command statement to test if 2+4 is less than 3 or 11.

» 2+4 < 3 | 11

ans =

1

»

The command statement above should be read: “Is the value of 2+4 less than 3 or 11.” Six is less than 11, so the operation is true and the result is 1. As with Relational operators, Logical operators are useful in performing comparisons and for decision making in programming loops.

I.4.6 Special Characters

In this section some special characters and their uses are explained. These special characters are helpful to know when constructing arrays, making comments in M-files, etc. The list of Special Characters and their functions are listed below.

[ ] Brackets are used to form vectors and Matrices

( ) Parentheses are used to indicate precedence in arithmetic expressions

= Used in assignment statements, ==is a Relational operator

‘ Matrix Transpose

. Decimal point

, Used to separate matrix subscripts and function arguments

; Used after expression to suppress printing

% Denotes comment

! Indicates the rest of the input line is a command to the operating system

You should have been exposed to most of these characters in the previous sections. However, since we have been stressing operators and the importance of precedence in arithmetic expressions, let us look at one more example.

Example I.4.6.1

In MATLAB write a command statement to test if 9 + 2*2 > 20. Then add parentheses around 9 + 2 to see the difference.

» 9 + 2*2 > 20

ans =

0

» (9 + 2)*2 > 20

ans =

1

»

As can be seen in the previous example, the parentheses changed the values on the left-hand side of the expression.

In this section we covered several important topics from the use of operators to the use of special characters. By going through the examples, the user should become more comfortable with MATLAB and more confident in writing M-files.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download