English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Operators

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. MATLAB is designed to run mainly on matrices and arrays. Therefore, MATLAB operators can handle both scalar data and non-scalar data. MATLAB allows the following types of basic operations-

  • Arithmetic operators

  • Relationship operators

  • Logical Operators

  • Bitwise operators

  • Set Operators

Arithmetic operators

MATLAB allows two different types of arithmetic operations-

  • Matrix arithmetic operations

  • Array arithmetic operations

Matrix arithmetic operations are the same as those defined in linear algebra. Array operations are performed on individual elements of one-dimensional and multi-dimensional arrays.

Matrix operators and array operators are distinguished by the dot (.) symbol. However, since the addition and subtraction operations of matrices and arrays are the same, the operators are the same in both cases. The following table briefly introduces the operators-

Example of arithmetic operators

Serial NumberOperator and Description
1

+

Addition or unary plus. A+B adds the values stored in the variables A and B. A and B must have the same size unless one of them is a scalar. Scalars can be added to matrices of any size.

2

-

Subtraction or unary minus. A-B subtracts the value of B from the value stored in the variable A and B. A and B must have the same size unless one of them is a scalar. Scalars can be subtracted from any size matrix.

3

*

Matrix multiplication. C = A*B is the linear algebraic product of matrices A and B. More accurately,

For non-scalar A and B, the number of columns of A must be equal to the number of rows of B. Scalars can be multiplied with matrices of any size.

4

.*

Array multiplication. A.*B is the element-wise product of arrays A and B. A and B must have the same size unless one of them is a scalar.

5

/

Backslash or matrix right division. B/A and B*inv(A) is roughly the same. More accurately, B/A = (A'\B')'.

6

./

Array right division. A./B is the element A(i,j)/Matrix B(i,j). A and B must have the same size unless one of them is a scalar.

7

\

Backslash or matrix left division. If A is a square matrix, then A\B is equivalent to inv(A)*B is roughly the same, but the calculation method is different. If A is an n by n matrix, and B is a column vector containing n components, or a matrix containing multiple such columns, then X = A\B is the solution to the equation AX = B. If A is ill-conditioned or nearly singular, a warning message will be displayed.

8

.\

Array left division. A.\B is the element B(i,j)/Matrix A(i,j). A and B must have the same size unless one of them is a scalar.

9

^

Matrix power. If p is a scalar, then X^p is the power of p. If p is an integer, the power is calculated by repeated squaring. If the integer is negative, X is first inverted. For other values of p, the calculation involves eigenvalues and eigenvectors, so if [V, D] = eig(X), then X^p = V*D.^p/V.

10

.^

Array power. A.^B is the matrix where the element A(i,j) is raised to the power of B(i,j). A and B must have the same size unless one of them is a scalar.

11

'

Matrix transpose. A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.

12

'.

Array transpose a' is the transpose of array a. For complex matrices, this does not involve conjugation.

Relationship operators

Relationship operators can also handle scalar and non-scalar data. The relationship operators of arrays perform element-wise comparison between two arrays and return a logical array of the same size, where the elements are set to logical1(true), the relationship is true, the elements are set to logical 0(false). Not.

The following table shows the relationship operators available in MATLAB-

Relationship operator examples

Serial NumberOperator and Description
1

<

less than

2

<=

less than or equal to

3

>

greater than

4

>=

greater than or equal to

5

==

equals

6

!=

Not equal

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 operate on scalars and logical expressions.

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

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

Example of Logical Operations

Bitwise Operations

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

pqp&qp | qp ^ Q
00000
01011
11110
10011

Assuming A = 60; And B = 13; Now in binary format, they will be 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 bitwise operations, such as bitwise AND, OR, and NOT operations, shift operations, etc.

The following table shows commonly used bitwise operations-

Example of Bitwise Operations

Function
Function
bitand(a, b)

Bitwise AND of integers a and b

bitcmp(a)

The two's complement of a

bitget(a,pos)

Get the bit at the specified position in the integer array a

bitor(a, b)

Bitwise OR of integers a and b

bitset(a, pos)

Sets the bit at a specific position pos of a

bitshift(a, k)ReturnsOneBy shifting leftķBit, equivalent to multiplying2 ķ.The negative value of k corresponds to right shift or division by2 | k | And round to the nearest integer towards negative infinity.Any overflow bits will be truncated.
bitxor(a, b)

Bitwise XOR of integers a and b

swapbytes

Bitwise XOR of integers a and b

Set Operators

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

The following table shows some commonly used set operations-

Example of Set Operations

Serial NumberFunction Description
1

intersect(A,B)

Sets the intersection of two arrays; returns the common values of A and B. The returned values are arranged in order.

2

intersect(A,B,'rows')

Treat each row of A and each row of B as a single entity, and return the common rows of A and B. The rows of the returned matrix are arranged in sorted order.

3

ismember(A,B)

Returns an array of the same size as A, containing1(true),where the elements of A are located in B. Elsewhere, it returns 0(false).

4

ismember(A,B,'rows')

Treat each row of A and each row of B as a single entity, and return an array containing1If the vector (true) where the rows of matrix A are also the rows of B. Elsewhere, it returns 0(false).

5

issorted(A)

If the elements are sorted in order, then return logical1(true) if the elements are sorted in order, otherwise return logical 0(false). The input A can be a vector or an N×1or1×N string cell array. If A and the output of sort(A) are equal, then A is considered to be sorted.

6

issorted(A, 'rows')

If the rows of the two-dimensional matrix A are sorted in order, then return logical1(true) if A and the output of sortrows(A) are equal, otherwise return logical 0(false).

7

setdiff(A,B)

Set the difference between two arrays; return the values in A that are not in B. The values in the array are sorted in order.

8

setdiff(A,B,'rows')

Treat each row of A and each row of B as a single entity, and return the rows of A that are not in B. The rows of the returned matrix are sorted in order.

The 'rows' option does not support cell arrays.

9

setxor

XOR of Two Arrays

10

union

Union of Two Arrays

11

unique

Unique Values in Arrays