An operator is a symbol that tells the compiler to perform various numerical or logical manipulations. MATLAB is designed to operate mainly on whole matrices and arrays. Therefore, functions in MATLAB work both on scalar and non-scalar data.
MATLAB has several types of operators, symbols, and special characters to deal with variables, functions, and arithmetic operations.
MATLAB Arithmetic Operators
Arithmetic operators help in performing simple arithmetic operations like addition, subtraction, multiplication, division, and power.
Arithmetic Operators and Arrays
Except for some matrix operators, MATLAB arithmetic operators work on corresponding functions of arrays with equal dimensions. For vectors and rectangular array, both operands must be the equivalent size unless one is a scalar. If one operand is a scalar and the other is not, MATLAB applies the scalar to every item of the other operand, this property is called scalar expansion.
This example uses scalar expansion to evaluate the product of a scalar operand and a matrix.
MATLAB Relational Operators
Relational operators perform value comparison operations.
Relational Operators and Arrays
The MATLAB relational operators compare corresponding components of arrays with equal dimensions. Relational operators always operate element-by-element. In this example, the resulting matrix present where the element of A is equal to the corresponding part of B.
For vectors and rectangular array, both operands must be the same size unless one is a scalar. In this case, where one operand is a scalar, and the other is not, MATLAB tests the scalar against every element of the other operand. Locations where the particular relation is true receive logical 1. Locations where the relation is false receive logical 0.
MATLAB Logical Operators
Logical operators perform logical operations and output the result in Boolean state true or false using the numbers 1 and 0, respectively.
MATLAB offer three types of logical operators and functions:
The values returned by MATLAB logical operators and functions, with the exception of bit-wise functions, are of type logical and are suitable for use with logical indexing.
Element-Wise Operators and Functions
The following logical operators and functions execute element-wise logical operations on their inputs to produce a like-sized output array. The examples are shown in the following table use vector inputs A and B, where
A = [0 1 1 0 1];
For operators and functions that take two array operands (&, |, and xor), both arrays must have the same dimensions, with each dimension being the same size. The one exception to this is where one operand is a scalar, and the other is not.
Note: MATLAB converts any finite nonzero, mathematic values used as inputs to logical expressions to logical 1, or true.
The following functions execute bit-wise logical operations on nonnegative integer inputs. Inputs may be scalar or in arrays. If in arrays, these operations produce a like-sized output array.
The examples are present in the following table use scalar inputs A and B, where
A = 28; % binary 11100
The following operators execute AND and OR operations on logical expressions, including scalar values. They are short-circuiting operators in that they calculate their second operand only when the first operand does not fully determine the output.
MATLAB Special Characters
Special characters perform some particular tasks according to their behavior and the position where they are used.
MATLAB String and Character Formatting Special Characters
There are some special characters to use only within the text of a character or string. These special characters are used to insert newlines or carriage returns, specify folder paths.