Beginner

Operators in MATLAB

Learn MATLAB operators including arithmetic, comparison, logical, and the critical distinction between matrix and element-wise operations

Operators are the building blocks of every MATLAB expression. Because MATLAB was designed around the matrix as its fundamental data type, its operators carry a distinction that most other languages lack: nearly every arithmetic operator has both a matrix form (*, /, ^) and an element-wise form (.*, ./, .^). Choosing the right one is essential, and getting it wrong is one of the most common sources of bugs for newcomers.

MATLAB’s array-first design means scalar operations automatically extend across entire matrices. Adding a scalar to a matrix adds it to every element. Multiplying two matrices with * performs true matrix multiplication, while .* multiplies element-by-element. This dual model gives MATLAB its expressive power for linear algebra while still supporting ordinary numeric computation.

In this tutorial you’ll learn the arithmetic, relational, and logical operators, how the element-wise variants differ from their matrix counterparts, how comparison produces logical arrays, and how short-circuit logical operators differ from their element-wise siblings.

Arithmetic Operators

MATLAB provides the standard arithmetic operators plus a parallel set of element-wise operators prefixed with a dot.

Create a file named operators_arithmetic.m:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
% Arithmetic operators in MATLAB
a = 10;
b = 3;

fprintf('a + b  = %d\n', a + b);    % Addition
fprintf('a - b  = %d\n', a - b);    % Subtraction
fprintf('a * b  = %d\n', a * b);    % Multiplication
fprintf('a / b  = %.4f\n', a / b);  % Right division
fprintf('a \\ b  = %.4f\n', a \ b); % Left division (b / a)
fprintf('a ^ b  = %d\n', a ^ b);    % Exponentiation
fprintf('mod    = %d\n', mod(a, b));% Modulo (function, not operator)

% Negation (unary minus)
disp(-a)

Notice MATLAB has both / (right division) and \ (left division). For scalars a \ b is simply b / a, but for matrices the left-division operator solves the linear system A*x = b — a uniquely MATLAB feature.

Matrix vs. Element-wise Operators

This distinction is the single most important operator concept in MATLAB.

Create a file named operators_matrix.m:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
% Matrix vs element-wise operations
A = [1 2; 3 4];
B = [5 6; 7 8];

disp('Matrix multiplication A * B:')
disp(A * B)

disp('Element-wise multiplication A .* B:')
disp(A .* B)

disp('Matrix power A^2 (equals A * A):')
disp(A^2)

disp('Element-wise power A.^2 (each element squared):')
disp(A.^2)

disp('Element-wise division A ./ B:')
disp(A ./ B)

A * B performs the standard linear-algebra dot-product across rows and columns. A .* B produces a matrix where each cell is A(i,j) * B(i,j). The two answers are completely different — the dot prefix is not optional decoration.

Comparison Operators

Comparison operators return logical arrays (matrices of 1 and 0) — not single booleans — when applied to vectors or matrices.

Create a file named operators_compare.m:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
% Comparison operators produce logical arrays
x = [1 2 3 4 5];
y = [5 4 3 2 1];

disp('x == y:'); disp(x == y)
disp('x ~= y:'); disp(x ~= y)   % "not equal" uses ~= , not !=
disp('x <  y:'); disp(x <  y)
disp('x >= y:'); disp(x >= y)

% Use the logical result as an index (logical indexing)
data = [10 -3 7 -8 4 -1];
positives = data(data > 0);
fprintf('Positive values: ');
fprintf('%d ', positives);
fprintf('\n');

MATLAB uses ~= for “not equal” — a holdover from its Fortran-influenced roots. Combined with logical indexing, comparison operators let you filter arrays without writing an explicit loop.

Logical Operators

MATLAB has two flavors of logical operators: element-wise (&, |, ~) which work on arrays, and short-circuit (&&, ||) which work on scalars and stop evaluating once the result is known.

Create a file named operators_logical.m:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
% Logical operators
a = true;
b = false;

% Short-circuit (scalars only): && and ||
fprintf('a && b = %d\n', a && b);
fprintf('a || b = %d\n', a || b);
fprintf('~a     = %d\n', ~a);     % Logical NOT

% Element-wise logical on arrays
v1 = [1 0 1 0];
v2 = [1 1 0 0];
disp('v1 & v2:'); disp(v1 & v2)
disp('v1 | v2:'); disp(v1 | v2)
disp('xor(v1,v2):'); disp(xor(v1, v2))

% Short-circuit prevents evaluating the right side
x = 0;
if x ~= 0 && (10 / x) > 1
    disp('safe division')
else
    disp('skipped division by zero')
end

Use && / || inside if conditions where short-circuit evaluation prevents errors. Use & / | when combining logical arrays element-by-element.

Assignment, Concatenation, and Precedence

Create a file named operators_misc.m:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
% Assignment is plain =
x = 5;

% MATLAB has NO compound assignment operators (no +=, -=, etc.)
x = x + 1;            % You must write it out
fprintf('x = %d\n', x);

% String concatenation: brackets join character arrays
first = 'Hello';
last  = 'MATLAB';
greeting = [first, ', ', last, '!'];
disp(greeting)

% Range operator creates vectors: start:step:stop
nums   = 1:5;          % 1 2 3 4 5
evens  = 0:2:10;       % 0 2 4 6 8 10
rev    = 10:-1:6;      % 10 9 8 7 6
disp(nums)
disp(evens)
disp(rev)

% Operator precedence: ^ before * before + ; parentheses always clarify
result = 2 + 3 * 4 ^ 2;   % 2 + 3 * 16 = 50
fprintf('2 + 3 * 4^2 = %d\n', result);

% Transpose operators
M = [1 2 3];
disp('Transpose M'':')
disp(M')               % .' is plain transpose, ' is conjugate transpose

A few MATLAB quirks worth highlighting: there are no +=/-= shortcuts (you must repeat the variable), the brackets [ ] double as a concatenation operator, the colon : builds ranges with optional step, and ' is the conjugate transpose while .' is the plain transpose — they differ only for complex numbers.

Running with Docker

1
2
3
4
5
6
7
8
9
# Pull the official image
docker pull gnuoctave/octave:9.4.0

# Run each example
docker run --rm -v $(pwd):/app -w /app gnuoctave/octave:9.4.0 octave --no-gui --no-window-system operators_arithmetic.m
docker run --rm -v $(pwd):/app -w /app gnuoctave/octave:9.4.0 octave --no-gui --no-window-system operators_matrix.m
docker run --rm -v $(pwd):/app -w /app gnuoctave/octave:9.4.0 octave --no-gui --no-window-system operators_compare.m
docker run --rm -v $(pwd):/app -w /app gnuoctave/octave:9.4.0 octave --no-gui --no-window-system operators_logical.m
docker run --rm -v $(pwd):/app -w /app gnuoctave/octave:9.4.0 octave --no-gui --no-window-system operators_misc.m

Expected Output

Output from operators_arithmetic.m:

a + b  = 13
a - b  = 7
a * b  = 30
a / b  = 3.3333
a \ b  = 0.3000
a ^ b  = 1000
mod    = 1
-10

Output from operators_matrix.m:

Matrix multiplication A * B:
   19   22
   43   50
Element-wise multiplication A .* B:
    5   12
   21   32
Matrix power A^2 (equals A * A):
    7   10
   15   22
Element-wise power A.^2 (each element squared):
    1    4
    9   16
Element-wise division A ./ B:
   0.2000   0.3333
   0.4286   0.5000

Output from operators_compare.m:

x == y:
  0  0  1  0  0
x ~= y:
  1  1  0  1  1
x <  y:
  1  1  0  0  0
x >= y:
  0  0  1  1  1
Positive values: 10 7 4 

Output from operators_logical.m:

a && b = 0
a || b = 1
~a     = 0
v1 & v2:
  1  0  0  0
v1 | v2:
  1  1  1  0
xor(v1,v2):
  0  1  1  0
skipped division by zero

Output from operators_misc.m:

x = 6
Hello, MATLAB!
   1   2   3   4   5
    0    2    4    6    8   10
   10    9    8    7    6
2 + 3 * 4^2 = 50
Transpose M':
   1
   2
   3

Key Concepts

  • Matrix vs. element-wise is MATLAB’s signature distinction: * / ^ apply linear-algebra rules, while .* ./ .^ operate on corresponding cells. Always think about which one you want.
  • Comparison produces logical arrays, not single booleans, when operating on vectors or matrices — enabling powerful logical indexing like data(data > 0).
  • ~= is “not equal” and ~ is the logical NOT — MATLAB never uses ! or !=.
  • Two logical families: && || short-circuit on scalars (safe inside if), while & | work element-wise across arrays.
  • No compound assignment — write x = x + 1, never x += 1. This is one of the most common surprises moving from C-family languages.
  • The colon : is an operator that builds ranges (start:step:stop) and selects entire dimensions in indexing (A(:,1)).
  • Left division \ is unique: for matrices it solves A*x = b directly — a feature no other mainstream language exposes as a built-in operator.
  • Two transposes: ' conjugates (for complex numbers), .' does not. For real matrices they produce identical results.

Running Today

All examples can be run using Docker:

docker pull gnuoctave/octave:9.4.0
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining