This note reviews some of the most widely used operators in MATLAB.

MATLAB operators

A complete list of MATLAB operators can be found here. Corresponding to each operator, there is also MATLAB function that does the same thing for you. Below is a summary of the most important operators and their corresponding functions in MATLAB.

Table of arithmetic operators in MATLAB.
Symbol Role Alternative
+ Addition plus()
+ Unary plus uplus()
- Subtraction minus()
- Unary minus uminus()
.* Element-wise multiplication times()
* Matrix multiplication mtimes()
./ Element-wise right division rdivide()
/ Matrix right division mrdivide()
.\ Element-wise left division ldivide()
\ Matrix left divison (also known as backslash) mldivide()
.^ Element-wise power power()
^ Matrix power mpower()
.' Transpose transpose()
' Complex conjugate transpose ctranspose()
Table of rational operators in MATLAB.
Symbol Role Alternative
== Equal toeq()
~= Not equal tone()
> Greater thangt()
>= Greater than or equal toge()
< Less thanlt()
<= Less than or equal tole()
Table of logical operators in MATLAB.
Symbol Role Alternative
& Logical ANDand()
| Logical ORor()
&& Logical AND (with short-circuiting)--
|| Logical OR (with short-circuiting)--
~ Logical NOTnot()

Logical operator short-circuit in MATLAB

Note that there are two types of logical operators, one of which is called short-circuit. More information about them and their meanings can be found here.

Special characters in MATLAB

There are also a set of characters that have a special role in MATLAB, some of which we have already discussed. You can learn about them here.

Type casting

In computer science, type conversion, type casting, or type coercion are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value/variable into a floating point value/variable or its textual representation as a string, and vice versa.

Implicit type casting and operator overloading

MATLAB is a loosely or weakly-typed language, which has a number of real-world implications. The first, and probably most notable, the difference between MATLAB and a strongly-typed language is that you don’t have to explicitly declare the types of the variables you use. For example, the declarations x=5; x=’foo’ immediately following one another are perfectly acceptable; the first declaration causes x to be treated as a number, the second changes its treatment to a string.

The main benefit of using a weakly-typed language is the ability to do rapid prototyping. The number of lines of code required to declare and use a dynamically allocated array in C (and properly clean up after its use) is much greater than the number of lines required for the same process in MATLAB. Weak typing is also good for code-reuse. You can code a scalar algorithm in MATLAB and with relatively little effort modify it to work on arrays as well as scalars. The fact that MATLAB is a scripted instead of a compiled language also contributes to rapid prototyping.

Weak typing is not without its pitfalls, though. For example, weak typing combined with unexpected user input can sometimes lead to erroneous results,

>> x=5;
>> y=4;
>> x/y
ans =

whereas the same code in strongly-typed compiled languages, such as C and Fortran would have given integer value 1.

Weak typing, combined with intrinsic MATLAB operator overloading can also lead to unexpected results when operations between mixed types are done. Consider what happens when you add 1+'1', that is, the scalar 1 and the character ‘1’. The surprising result is 50. MATLAB automatically converts the character '1' to its ASCII representation value, which is 49 (You can get the ASCII code for any character using the abs() function). After this implicit conversion, MATLAB adds the scalar value of 1 to return 50.

The situation can be more complicated when you deal with strings rather than single characters. For example,

>> x=10;
>> y='10';
>> x+y
ans =
    59    58

What happened above is that '10' is considered as a vector of characters, therefore, when you add it to 10, Matlab automatically converts this string to a vector of corresponding ASCII values and adds the value 10 to each one.

>> 'Hello' + 'World'
ans =
    159 212 222 216 211

Explicit type casting

Unlike implicit type casting which is done by MATLAB under the hood as described above, which can also be dangerous, you can also explicitly request MATLAB to convert the type of a variable to some other type, using MATLAB’s built-in functions like cast, or one of the many other functions that MATLAB has for this purpose.

Array vs. Matrix Operations

We have already learned that arrays and matrices are not the same in MATLAB. As a result, the operators that can act on them are also not identical.

Matrix operations follow the rules of linear algebra and are not compatible with multidimensional arrays. The required size and shape of the input matrices depend on the type of operation. For non-scalar inputs, the matrix operators generally calculate different answers than their array operator counterparts.

Array operations execute element by element operations on corresponding elements of vectors, matrices, and multidimensional arrays. If the operands have the same size, then each element in the first operand gets matched up with the element in the same location in the second operand. If the operands have compatible sizes, then each input is implicitly expanded as needed to match the size of the other.

You can learn more about them in MATLAB manual.

Operator precedence in MATLAB

You can build expressions that use any combination of arithmetic, relational, and logical operators. Precedence levels determine the order in which MATLAB® evaluates an expression. Within each precedence level, operators have equal precedence and are evaluated from left to right. The precedence rules for MATLAB operators are shown in this list, ordered from highest precedence level to lowest precedence level:

  1. Parentheses ().
  2. Transpose .', power .^, complex conjugate transpose ', matrix power ^.
  3. Power with unary minus .^-, unary plus .^+, or logical negation .^~ as well as matrix power with unary minus ^-, unary plus ^+, or logical negation ^~. (Note: Although most operators work from left to right, the operators ^-, .^-, ^+, .^+, ^~, and .^~ work from second from the right to left. It is recommended that you use parentheses to explicitly specify the intended precedence of statements containing these operator combinations.)
  4. Unary plus +, unary minus -, logical negation ~.
  5. Multiplication .*, right division ./, left division .\, matrix multiplication *, matrix right division /, matrix left division \.
  6. Addition +, subtraction -.
  7. Colon operator :.
  8. Less than <, less than or equal to <=, greater than >, greater than or equal to >=, equal to ==, not equal to ~=.
  9. Element-wise AND &.
  10. Element-wise OR |.
  11. Short-circuit AND &&.
  12. Short-circuit OR ||.

You can learn more about operator precedence in MATLAB manual.