MATLAB is primarily designed to operate on matrices and arrays. Hence, operators in MATLAB work on both scalar and non-scalar data. MATLAB supports the following basic types of operators:
- Arithmetic operators.
- Relational operators.
- Logical operators.
- Bitwise operations.
- Set operations.
Arithmetic Operators in MATLAB
MATLAB allows two types of arithmetic operators, including:
- Matrix arithmetic operators.
- Array arithmetic operators.
Matrix arithmetic operators are defined similar to linear algebra. Array operators are executed element-wise, both on one-dimensional arrays and multi-dimensional arrays.
Matrix and array operators are distinguished by the dot symbol (.). However, addition and subtraction operators in matrices and arrays are the same. The table below briefly describes the arithmetic operators in MATLAB:
Relational Operators in MATLAB
Relational operators can work on both scalar and non-scalar data. Array relational operators compare each element between two arrays and return a logical array of the same size, with elements set to 1 (true) where the relation is true and elements set to 0 (false).
Below is a table listing the relational operators in MATLAB:
Logical Operators
MATLAB offers 2 types of logical operators and functions:
- Element-wise: Operators operate on corresponding elements of logical arrays.
- Short-circuit: Operators operate on scalar and logical expressions.
Element-wise logical operators perform operations on each element of logical arrays. The symbols &, |, and - are logical array operators for AND, OR, and NOT operations, respectively.
Short-circuit logic operators in MATLAB allow skipping logic computations. The characters && and || are short-circuit logical operators for AND and OR operations, respectively.
Bitwise Operations in MATLAB
Bitwise operators work on bits and execute one bit at a time. Below is a table listing the operations &, |, and ^ in bitwise operators:
Assuming A = 60 and B = 13. In binary form, these values are represented 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 performing operations on bits such as 'bitwise and', 'bitwise or', and 'bitwise not', ... .
Below is a table listing common Bitwise operators:
Set Operators in MATLAB
MATLAB provides various set operators such as union, intersect, ....
Below is the table listing the set operators in MATLAB:
Thus, the Mytour article has just introduced you to the operators in MATLAB. Once you've familiarized yourself with operators in MATLAB, you can explore further how to add mathematical symbols in MATLAB to enhance your MATLAB knowledge. Mytour's upcoming articles will continue to introduce you to array declaration, commands in MATLAB, .... Stay tuned for new articles on Mytour!