Arithmetic Operators

Matlab uses the standard arithmetic operators and a couple of additional operators to allow users the ability to direct which operations occur and in what order for solving engineering problems. The arithmetic operators have the standard order of precedence that you find on an electronic calculator.

Operators of equal precedence are evaluated from left to right.

Note: Be sure to review the power, transpose, and colon operators for some operations that might not be familar to you.

Matlab Operator Precedence Table
Operator
Description
Precedence
( )
Use parentheses to force a particular evaluation order
operator precedence: highest to lowest
.^ , .' , ^ , ' Element-wise Power (.^), Element-wise Transpose (.'), Power (^) and Complex conjugate transpose (')
+ , - , ~ Unary plus (+), unary minus (-), logical negation (~)
* , / , \ Element-wise Multiplication (.*) , Element-wise Division (./), and Matrix Multiplication (*), Division (/), and Left division (\)
Left division is described in "Matrices and Linear Algebra" in the MATLAB documentation.
+ , -
Addition (+) and Subtraction (-)
:
Colon (:) operator. Used for creating new vectors and accessing elements in existing vectors.
=
Assignment (=) operator. Used for assigning new values to existing (or new) matrices.
; Suppress output (;) operator. Used for suppressing the output of the statement. The statement is still executed, but nothing is displayed to the command window after execution.
Note: There are relational and logical operators that will be introduced and added to the precedence table in a later module and lesson.

The Colon (:) Operator

The colon operator deserves special mention. It is used to conveniently create a vector (a single dimension array). The idea is to give Matlab the first value, and increment amount, and the last value and have it generate the values in between. This is possible when all values have a uniform difference between each successive value.

For example, to create a vector named digits that contains the values [ 0 1 2 3 4 5 6 7 8 9 ], we can use the colon operator as follows:

digits = 0 : 1 : 9

or we may omit the middle value and second : if the increment value is one. The digits vector can also be created with this statement.

digits = 0:9

The default is to increment by positive one from the start value to the end value. Use two colons and an increment value if the increment from one value to the next is something other than positive one. The middle term is the increment value as shown here to create a vector with all positive even values between 2 and 100, inclusive.

even_numbers = 2:2:100

Element-Wise (Scalar) Operations

Suppose we wish to square each element in a matrix named A. At first we might try to type:

    A^2

However, this will result in the square of the matrix A, i.e. the matrix multiplication A x A. To square each element in the matrix we must use the element-wise multiplication operator.

    A .^ 2

The period before the ^ operator tells Matlab to do the power operation element-wise. Element-wise division and multiplication can also be done, e.g.

    A ./ B
    A .* B

Note that we do not need to use this special notation to do element-wise addition or subtraction. This is because matrix addition and subtraction are already, by definition, done element-wise. Also, it is not necessary to use element-wise operators when multiplying arrays and constants or when dividing arrays by constants.

Element-wise operations are also called scalar operations. The ability to do scalar operations is important for many situations as we will see in the following example.