Programming

Operators

The MATLAB operators fall into three categories:

• Arithmetic Operators perform numeric computations, for example, adding two numbers or raising the elements of an array to a given power.
• Relational Operators compare operands quantitatively, using operators like "less than" and "not equal to."
• Logical Operators use the logical operators AND, OR, and NOT.

This section also discusses Operator Precedence.

Arithmetic Operators

MATLAB provides these arithmetic operators.

 Operator Description `+` Addition `-` Subtraction `.*` Multiplication `./` Right division `.\` Left division `+` Unary plus `-` Unary minus `:` Colon operator `.^` Power `.'` Transpose `'` Complex conjugate transpose `*` Matrix multiplication `/` Matrix right division `\` Matrix left division `^` Matrix power

Arithmetic Operators and Arrays

Except for some matrix operators, MATLAB arithmetic operators work on corresponding elements of arrays with equal dimensions. For vectors and rectangular arrays, both operands must be the same size unless one is a scalar. If one operand is a scalar and the other is not, MATLAB applies the scalar to every element of the other operand -- this property is known as scalar expansion.

This example uses scalar expansion to compute the product of a scalar operand and a matrix.

• ```A = magic(3)
A =
8     1     6
3     5     7
4     9     2

3 * A
ans =
24     3    18
9    15    21
12    27     6
```

 Special Values Relational Operators