Operators:
An operator is a symbol in computer programming and mathematics which is used to perform specific mathematical or logical operations or you can also define it as a character that represents an action for example ‘+’ is a character that represents addition similarly Boolean operators are used to working with true/false values.
One more important fact to keep in mind is operators in MATLAB work for both scalar and non-scalar data.
Types of Operators:
MATLAB offers the following type of operators:
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Set operators
1. Arithmetic Operators:
Matlab provides two types of Arithmetic operators
- Matrix arithmetic operators
- Array arithmetic operators
As the above two types suggest Arithmetic Operators used with one or more operands as arrays are Array arithmetic operators and with one or more operand as a matrix is matrix arithmetic operator. The difference between them is the use of (.) symbol along with the operator in the case of Array arithmetic operators.
- Addition Operator (+): This operator is used to add two values as the name suggests. A few things that we need to keep in mind while using this that both the values should be of the same size unless one of them is scalar.
- Subtraction operator (-): This operator is used to subtract two values as the name suggests. A few things that we need to keep in mind while using this that both the values should be of the same size unless one of them is scalar.
- Matrix Multiplication: If C= A*B then C is the linear algebraic product of A and B. If both the operands are non-scalar then this operation can only happen if the number of columns in A is equal to a number of rows in B.
- Array Multiplication(.*): It is the element by element multiplication of two arrays for eg C= A.*B and both A and B should be of the same size.
- Matrix right division (/): For example, B/A is roughly the same as B*inv(A) where B and A are two matrices.
- Array right division (./): The only difference is, in this case, it will B./A where both A and B must have the same size unless one of them is scalar.
- Matrix Left division (\): In this type of matrix multiplication, A is a by n matrix and B is a column vector. The solution to AX= B is X=A\B.
- Array Left division (.\): A.\B is the matrix with elements B(i,j) \a(I,j) .A and B must have the same size.
- Matrix power(^): X^p is X to the power p if p is scalar or if p is an integer the power is computed by repeated squaring.
- Array power (.^): ^p is matrix X to the power matrix unless both of them are scalar. Both must have the same size.
- Matrix transpose (‘): If A is a matrix then A’ will be its linear algebraic transpose and if A is complex then A’ will be a complex conjugate transpose of it.
- Array transpose (.’): If A is an array then A.’ is the transpose of A but for complex array A there is no complex conjugate like matrix transpose.
2. Relational Operators:
These types of Operators can work with both scalar and non-scalar data. As the name suggests it finds a relation between each element of two arrays and if a relation exists then it returns true or else false. The operator returns an array of the same size with values true and false depending on the result of an operation.
- < Less than
- <= Less than equal to
- > Greater than
- >= Greater than or equal to
- == Equal to
- ~= Not equal to
3. Logical Operators:
Matlab provides two types of Logical Operators are as given below:
- Element-wise: Element-wise operator operates on elements of logical arrays. The symbols used in these operators are: & (AND), |(OR) ~ (NOT)
- Short-circuit: These types of operators work on scalar and logical operations. The symbols && and || are the logical short circuit operators AND and OR.
4. Bitwise Operators
As it is clear by the name Bitwise Operators work on a bit-by-bit operation. The Bitwise Operator symbols are |, &, and ^: The truth table is as follows:
P | Q | P & Q | P | Q | P ^ Q |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Interestingly, MATLAB provides various functions for bitwise and, bitwise or, bitwise not operations and shift operation, etc.
Matlab provides the following bitwise operators
- bit and(a,b) – Bitwise AND of integers a and b
- bitmap(a) – Bitwise complement of a
- bitget(a, pos) – Get bit at a specified position, in the array a
- bitset(a, pos) – set bit at a specified location of a
- bitShift(a,k) – It is equivalent to get multiplied by 2k.So, if k is negative then it shifts right and if k is positive then it shifts left.
- bitor(a, b) – Bit-wise XOR of integers a and b
5. Set Operators
MATLAB provides various set operators like a union, intersection, etc. Please find below the various list of operators.
- intersect(A & B): It is used to intersect A and B and returns the common values of A and B in sorted order.
- intersect(A & B, ‘rows’): It returns rows common to both A and B.
- ismember(A, B): It returns an array of size A with 1 for all elements of A are found in B or else none.
- Issorted(A): It returns 1 if elements are in sorted order and 0 if not.
- union: It sets union of two arrays
- unique: Gives unique values in an array.
Logical Operators:
Logical operators are the types of operators that result in binary values i.e. 1 or 0 depending on the inputs given to the expression. They are also used in arrays and conditional statements to check various conditions and statements. There are 2 types of logical operations that are used in Matlab. Please find them below:
- Element wise Logical Operation: Here the logical operation is performed between the operands element-wise. & and | are used to denote element-wise operations in Matlab.
- Short Circuit Logical Operation: Here the logical operation results in a scalar value depending on evaluating the first expression only. After the first part of the expression is evaluated, it is short-circuited, hence the name. && and || are used to denote the short circuit behavior in Matlab.
Types of Logical Operators with Examples
There are three types of logical operators that are used in Matlab like AND(E&F), OR(E|F), NOT(~E). Please find below the working and types of Logical Operators used in Matlab:
1. Logical AND Operator
In element-wise operation, it is denoted by & operator. It performs logical operation and results in 1 or 0(True or False) depending on the inputs provided to the input signal. Please find the below truth table which describes the working of AND operator in Matlab:

In the above truth table, if any of the input or operand is 0 or false then the resulting output is always False or 0. Similarly, if both the input signals are True then the resulting output is True. In the short-circuiting operation, the expression results in output by evaluating the first part of the expression. If the first part of the expression results in 0 or False, then the second part of the defined expression is not evaluated more. The output of the expression using the short circuit logical operation always result in a scalar value. It is denoted by && operator in Matlab. We should always be careful while using && and & operator in Matlab since both the operators are different and will give different outputs.
Example:
E = [0,0,1,1,0,1,0]
F = [1,1,1,0,0,0,1]
Output:
E&F = [0,0,1,0,0,0,0]
If both the inputs of the above array are 1 then it will result in 1 else 0.
2. Logical OR Operator
In element-wise operation, it is denoted by | operator. It performs logical operation and results in 1 or 0(True or False) depending on the inputs provided to the input signal. Please find the below truth table which describes the working of OR operator in Matlab.

In the above truth table, if any of the input or operand is 1 or True then the resulting output is always True or 1. Similarly, if both the input signals are True then the resulting output is True. In the short-circuiting operation, the expression results in output by evaluating the first part of the expression. If the first part of the expression results in 1 or True, then the second part of the defined expression is not evaluated more. The output of the expression using the short circuit logical operation always result in a scalar value. It is denoted by || operator in Matlab. We should always be careful while using || and | operator in Matlab since both the operators are different and will give different outputs.
Example:
<!-- wp:paragraph -->
<p>E = [1,1,1,1,0,0,0]</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>F = [0,0,1,0,1,0,0]</p>
<!-- /wp:paragraph -->
Output:
E|F = [1,1,1,1,1,0,0]
If any of the operands is 1 then it results in 1 else 0.
3. Logical NOT Operator
This operator returns 0 or 1 depending on the input we provide to the input signals. If the input signal is 0, then it results in 1 and if the input signal is 1 then it results in 0. The input can be a multi-dimensional array, scalar, matrix or vector. It is denoted by ~ sign in Matlab. It also supports complex numbers. not(E) is also used to denote the Logical NOT operation in Matlab but it is avoided since it has operator overloading issue. Please find the below Truth table to describe the working of Logical NOT operator in Matlab:

Example:
E = [1,0,1,0,1,0,1]
Output:
~E = [0,1,0,1,0,1,0]
It simply negates the input and provided the output.
There are many predefined functions that are used in logical operations in Matlab like:
- any(E): This function is used to check whether the elements in an array are logical 1 or non-zero. If the array contains any non-zero element, then it results in 1 else 0.
- all(E): This function is used to check whether all elements in an array is non-zero or 1. If all the elements in an array are non-zero or 1 then it results as scalar value as 1 for that respective array, if not then 0.
- Islogical(): This function is used to check if a particular array is logical or not. If the expression is logical then it returns True Else False.