MATLAB Operators: A Comprehensive Guide

Operators are symbols that tell the compiler to perform specific mathematical or logical operations. MATLAB is primarily used for operations on entire matrices and arrays. Therefore, operators in MATLAB can be used for both scalar and non-scalar data. MATLAB allows the following types of basic operations:

  • Arithmetic Operators

  • Relational Operators

  • Logical Operators

  • Bitwise Operators

  • Set Operators

Let’s learn about each one.

Arithmetic Operators

MATLAB allows two different types of arithmetic operations –

  • Matrix arithmetic operations

  • Array arithmetic operations

Matrix arithmetic operations are defined the same way as in linear algebra. In one-dimensional and multi-dimensional arrays, array operations are performed element-wise. Matrix operators and array operators are distinguished by the dot notation (.). However, since the addition and subtraction operations for matrices and arrays are the same, the operators are the same for both cases. The table below briefly describes the arithmetic operators:

MATLAB Operators: A Comprehensive Guide

Relational Operators

Relational operators can also be used for both scalar and non-scalar data. The relational operators for arrays perform element-wise comparisons between two arrays and return a logical array of the same size, where elements are set to logical 1 (true) if true, and 0 (false) if false.

The table below shows the relational operators available in MATLAB:

Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to

Logical Operators

MATLAB provides two types of logical operators and functions:

  • Element-wise – These operators operate on the corresponding elements of logical arrays.

  • Short-circuit – These operators run on scalar and logical expressions.

Element-wise logical operators operate on logical arrays element-wise. The symbols &, |, and ~ are the logical array operators AND, OR, and NOT.

Short-circuit logical operators allow logical operations to short-circuit. The symbols && and || are the logical short-circuit operators AND and OR.

Bitwise Operations

Bitwise operators operate on bits and perform bitwise operations. The truth tables for &, |, and ^ are as follows –

MATLAB Operators: A Comprehensive Guide

Assuming A = 60 and B = 13; in binary format, they are as follows:

A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A  = 1100 0011

MATLAB provides various functions for bit operations such as “bitwise AND”, “bitwise OR”, “bitwise NOT”, and shift operations.

The table below shows some common bitwise operations:

MATLAB Operators: A Comprehensive Guide

Set Operations

MATLAB provides various functions for set operations, such as union, intersection, and membership testing of sets.

The table below shows some common set operations:

MATLAB Operators: A Comprehensive Guide

Leave a Comment