Skip to content

MATLAB - Operators

An operator is a symbol that instructs the compiler to perform a specific mathematical or logical manipulation. MATLAB’s operators are designed to work seamlessly on both scalars (single numbers) and arrays/matrices, making it a powerful tool for numerical computation.

MATLAB’s elementary operations can be categorized as follows:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operations
  • Set Operations

A crucial concept in MATLAB is the distinction between matrix arithmetic and array arithmetic. Array operations, denoted by a preceding dot (.), are performed element-by-element.

OperatorDescription
+Addition. A + B. Works element-wise.
-Subtraction. A - B. Works element-wise.
*Matrix multiplication. Follows the rules of linear algebra. The number of columns in A must equal the number of rows in B.
.*Array (element-wise) multiplication. A .* B. A and B must have the same dimensions, or one must be a scalar.
/Matrix right division. A / B is roughly equivalent to A * inv(B).
./Array (element-wise) right division. A ./ B. Divides each element of A by the corresponding element of B.
\Matrix left division. A \ B is roughly equivalent to inv(A) * B. This is the recommended way to solve the linear system Ax = B.
.\Array (element-wise) left division. A .\ B. Divides each element of B by the corresponding element of A.
^Matrix power. A^p computes A to the power of p.
.^Array (element-wise) power. A .^ B. Raises each element of A to the corresponding power in B.
‘Matrix transpose. A' is the linear algebraic transpose. For complex matrices, it is the complex conjugate transpose.
.’Array transpose. A.' transposes A without complex conjugation.
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
% Matrix multiplication (1*5 + 2*7 = 19)
C_matrix = A * B
% Array (element-wise) multiplication (1*5=5, 2*6=12, ...)
C_array = A .* B

Output:

C_matrix =
19 22
43 50
C_array =
5 12
21 32

Relational operators compare arrays element by element and return a logical array of the same size, with 1 (true) or 0 (false) for each element.

OperatorDescription
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to
==Equal to
~=Not equal to
A = [1, 5, 6];
B = [2, 5, 3];
% Find elements where A is greater than B
L1 = A > B
% Find elements where A is equal to B
L2 = A == B

Output:

L1 = 1x3 logical array
0 0 1
L2 = 1x3 logical array
0 1 0

MATLAB has two types of logical operators:

  • Element-wise (&, |, ~): Operate on each element of logical arrays.
  • Short-circuit (&&, ||): Operate on scalar logical expressions and are used in control flow statements like if and while. They evaluate the second operand only if the result is not fully determined by the first.
% Element-wise AND
A = [1, 0, 1]; % Using 1 and 0 for true and false
B = [0, 0, 1];
C = A & B % Returns a logical array
% Short-circuit AND in an if statement
x = 5;
y = 10;
if (x > 0) && (y > 5)
disp('Both conditions are true.');
end

Output:

C = 1x3 logical array
0 0 1
Both conditions are true.

Bitwise operators perform bit-by-bit operations on integer types.

FunctionPurpose
bitand(a, b)Bit-wise AND of integers a and b
bitor(a, b)Bit-wise OR of integers a and b
bitxor(a, b)Bit-wise XOR of integers a and b
bitcmp(a)Bit-wise complement of a
bitshift(a, k)Shifts bits. Positive k is left shift, negative is right shift.
% A = 60 (binary 00111100)
% B = 13 (binary 00001101)
a = uint8(60);
b = uint8(13);
% Result should be 12 (binary 00001100)
result = bitand(a, b)

Output:

result = 12

MATLAB provides functions to perform set operations on vectors.

FunctionDescription
union(A,B)Set union; returns elements that are in A or B, sorted.
intersect(A,B)Set intersection; returns elements common to both A and B.
setdiff(A,B)Set difference; returns elements in A that are not in B.
setxor(A,B)Set exclusive OR; returns elements not in the intersection of A and B.
unique(A)Returns the unique elements of A.
ismember(A,B)Returns a logical array indicating which elements of A are also in B.
A = [1, 2, 3, 5];
B = [2, 4, 5, 6];
% Union of A and B
C_union = union(A, B)
% Intersection of A and B
C_intersect = intersect(A, B)

Output:

C_union =
1 2 3 4 5 6
C_intersect =
2 5