Operators and Expressions

The dot operators are the single most important MATLAB concept for beginners: [1 2 3] * [1 2 3] is an error (inner dimensions don't match), [1 2 3] .* [1 2 3] multiplies element by element.

Concept

Addition, subtraction Matrix multiplication, right division, left division, matrix power

Element-wise multiplication, division, power Complex-conjugate transpose / plain transpose

The dot operators are the single most important MATLAB concept for beginners: [1 2 3] * [1 2 3] is an error (inner dimensions don't match), [1 2 3] .* [1 2 3] multiplies element by element.

a \ b means "a divided *into* b" — for scalars 2 \ 7 = 3.5; for matrices it solves linear systems (Module 6).

Relational and Logical ==, ~=, <, <=, >, >= return logical arrays. Logical operators: &, |, ~ (element-wise) and &&, || (short-circuit, scalar only — use them in if conditions), plus xor, any, all.

Precedence (highest first) Parentheses → transpose and power (', ^, .^) → unary + - ~ → multiplication/division → addition/subtraction → colon : → relational → & → | → && → ||.

Note: -2^2 is -4 because power binds tighter than unary minus.

Rounding and Remainders round (to nearest, halves away from zero), floor, ceil, fix (toward zero). mod(a, m) has the sign of m; rem(a, m) has the sign of a.

Why It Matters

Using * instead of .* (or ^ instead of .^) is the most common MATLAB bug — it either errors or, worse, silently computes a matrix product.

Syntax

y = x .^ 2 + 3 .* x - 1;        % element-wise polynomial
ok = (v >= 11.5) & (v <= 14.8); % element-wise logical
if n > 0 && data(n) > 0, ..., end

Example

3.1 Operators, precedence and rounding

a = 7; b = 2;
fprintf('a + b = %g, a - b = %g, a * b = %g, a / b = %g\n', a + b, a - b, a * b, a / b)
fprintf('a ^ b = %g, mod(a, b) = %g, rem(-7, 2) = %g, mod(-7, 2) = %g\n', a ^ b, mod(a, b), rem(-7, 2), mod(-7, 2))
fprintf('b \\ a (left division) = %g\n', b \ a)

% relational and logical operators return logical 1/0
fprintf('a > b: %d, a == b: %d, a ~= b: %d\n', a > b, a == b, a ~= b)
fprintf('(a > 5) && (b > 5): %d, (a > 5) || (b > 5): %d, ~true: %d, xor(true, false): %d\n', ...
        (a > 5) && (b > 5), (a > 5) || (b > 5), ~true, xor(true, false))

% precedence: ^ before unary minus before * / before + -
fprintf('-2^2 = %g, (-2)^2 = %g, 2 + 3 * 4 = %g, (2 + 3) * 4 = %g\n', -2^2, (-2)^2, 2 + 3 * 4, (2 + 3) * 4)

% matrix vs element-wise operators
v = [1 2 3];
disp('v .* v ='); disp(v .* v)
disp('v .^ 2 ='); disp(v .^ 2)
disp('v * v'' (inner product) ='); disp(v * v')

% rounding family
w = -2.5;
fprintf('round(%g) = %g, floor = %g, ceil = %g, fix = %g\n', w, round(w), floor(w), ceil(w), fix(w))

Expected Result

rem(-7, 2) is −1 but mod(-7, 2) is 1. -2^2 is −4. v * v' is the inner product 14, while v .* v squares each element. round(-2.5) rounds away from zero to −3.

a + b = 9, a - b = 5, a * b = 14, a / b = 3.5
a ^ b = 49, mod(a, b) = 1, rem(-7, 2) = -1, mod(-7, 2) = 1
b \ a (left division) = 3.5
a > b: 1, a == b: 0, a ~= b: 1
(a > 5) && (b > 5): 0, (a > 5) || (b > 5): 1, ~true: 0, xor(true, false): 1
-2^2 = -4, (-2)^2 = 4, 2 + 3 * 4 = 14, (2 + 3) * 4 = 20
v .* v =
   1   4   9
v .^ 2 =
   1   4   9
v * v' (inner product) =
14
round(-2.5) = -3, floor = -3, ceil = -2, fix = -2

Common Mistakes

* or ^ where .* or .^ is intended.

Using &&/|| with arrays (error: operands must be convertible to logical scalars).

if x == 1 | 2 — always true, because 2 is nonzero; write if x == 1 || x == 2 or ismember(x, [1 2]).

Forgetting that ' is the conjugate transpose for complex data (use .' to transpose without conjugating).

Best Practices

Default to dot operators for element-wise math on vectors.

Use parentheses whenever precedence isn't obvious.

Use &&/|| in conditions (short-circuit protects expressions like n > 0 && x(n) > 0).

Interview Relevance

High for MATLAB roles: "* vs .*?", "& vs &&?", "mod vs rem?", "what is A \ b?".

Practice Questions

Evaluate 2^3^2 and explain the result.

Compute y = 3x^2 - 2x + 1 for x = 0:0.5:2 with one statement.

Write a condition that is true when x is between 10 and 20 inclusive.

Compare round, floor, ceil and fix on [-1.5 -0.5 0.5 1.5].

Quick Quiz

Q1. (Multiple choice) Which multiplies two vectors element by element? A) a * b B) a.* b C) a b D) dot(a, b) Answer: B. Q2. (Predict the output) -2^2 Answer:** -4 (verified).

Q3. (True/False) mod(-7, 2) and rem(-7, 2) return the same value.

Answer: False — 1 and −1 (verified).

Q4. (Find the bug) if x == 1 | 2 runs even when x is 5.

Answer: It is (x == 1) | 2, and 2 is true. Use x == 1 || x == 2.

Coding Challenge

Compute the power P = V .* I and energy over 10 seconds for voltage samples [12.6 12.4 12.5] V and current samples [2.0 2.5 1.5] A, printing the power per sample and the total energy if each sample lasts 10/3 s.

V = [12.6 12.4 12.5];
I = [2.0 2.5 1.5];
P = V .* I;                         % element-wise product
dt = 10 / 3;
fprintf('power per sample: %s W\n', mat2str(P))
fprintf('energy: %.2f J\n', sum(P) * dt)