Operators
MATLAB’s operators have two faces: a matrix face, in which * is the linear-algebraic matrix product and / solves a linear system; and an element-wise face, in which the dotted forms .*, ./, .^ act independently on each element. The dual treatment is the language’s defining feature and is the substance of most idiomatic MATLAB code: a programmer learns to read A*B as matrix product, A.*B as Hadamard product, and to remember which sense is wanted at each operator.
Arithmetic operators
| Operator | Matrix sense | Element-wise sense |
|---|---|---|
+ | Addition | (the matrix and element-wise senses coincide) |
- | Subtraction | (likewise) |
* | Matrix product A*B | .* Hadamard product A.*B |
/ | Right divide A/B = A*inv(B) | ./ Element-wise divide |
\ | Left divide A\b (solves Ax = b) | .\ Element-wise left-divide |
^ | Matrix power A^n | .^ Element-wise power |
' | Conjugate transpose | .' Plain transpose |
- (unary) | Negation | (matrix and element-wise coincide) |
The left-divide \ deserves its own paragraph. A \ b solves the linear system A*x = b for x and is, in numerical practice, the canonical way to not compute inv(A): it is faster, more accurate, and more memory-efficient. The operator dispatches on the structure of A (triangular, symmetric, sparse, square, rectangular) to the appropriate LAPACK routine. For a rectangular A, A \ b returns the least-squares solution.
A = [1 2; 3 4];
b = [5; 6];
x = A \ b % solves A*x = b
% x = [-4; 4.5]
The transpose operators are also a pair: ' is the conjugate transpose (correct for complex matrices in most linear-algebra contexts), and .' is the plain transpose. The two coincide for real arrays. Idiomatic real-only code uses ' for brevity; complex-aware code uses .' deliberately.
Element-wise arithmetic
A = [1 2 3];
B = [10 20 30];
A + B % [11 22 33]
A .* B % [10 40 90]
A ./ B % [0.1 0.1 0.1]
A .^ 2 % [1 4 9]
2 .^ A % [2 4 8]
The leading dot is the element-wise marker. The reader of MATLAB learns to scan for dots; A*B and A.*B mean entirely different things, and confusing them is a common bug. The error “matrix dimensions must agree” from A*B where A.*B was meant is the typical diagnostic.
Comparison operators
| Operator | Meaning |
|---|---|
== | Element-wise equality |
~= | Element-wise inequality |
<, <=, >, >= | Element-wise ordering |
Comparison operators are always element-wise and return a logical array of the same shape as their operands (with implicit expansion — see Implicit expansion). To test whether two arrays are equal as a whole, the convention is isequal(A, B):
A = [1 2 3];
A == [1 2 4]
% 1×3 logical: [1 1 0]
isequal(A, [1 2 4])
% false
Note that == between strings (== between string scalars) tests string equality and returns a scalar logical, whereas == between char arrays of equal length tests character-by-character equality and returns a logical array. This is the most-cited difference between the two text classes.
Logical operators — short-circuit vs element-wise
MATLAB distinguishes two pairs:
| Operator | Behaviour |
|---|---|
& | Element-wise AND (over logical arrays) |
| ` | ` |
&& | Short-circuit AND (operands must be scalar logical) |
| ` | |
~ / not | Logical NOT |
The doubled && and || evaluate their right operand only if the result is not already determined, and they require scalar operands; they are the right choice in if conditions and other places where short-circuit semantics matter. The single & and | evaluate both sides and operate element-wise over arrays; they are the right choice when combining logical arrays:
x = 1:10;
mask = (x > 3) & (x < 8); % element-wise; mask is logical [0 0 0 1 1 1 1 0 0 0]
x(mask) % [4 5 6 7]
if isempty(x) || x(1) > 0 % short-circuit; safe because isempty is checked first
...
end
The mistake of using && where & was meant — “Operands to the || and && operators must be convertible to logical scalar values” — is a common beginner error.
Indexing as an operator
Parentheses (), curly braces {}, and the dot . form MATLAB’s indexing system. They are described at length in Indexing and slicing; for the purposes of this page, note that all three are expressions that yield values:
A(2, 3) % parenthesis indexing into a numeric array
A(2:end, :) % slice
c{1} % brace indexing into a cell array (contents)
s.field % dot indexing into a struct
The keyword end inside an index expression refers to the last index of the enclosing dimension and is the language’s most-used convenience.
Operator precedence
Precedence runs roughly from tightest to loosest:
(),{},.',.',^,.^(transpose and power)- unary
+, unary-,~ *,/,\,.*,./,.\+,-:(range constructor)<,<=,>,>=,==,~=&|&&||=(assignment)
The rules are mostly conventional with two surprises: the colon operator binds looser than arithmetic, so 1:n-1 is 1:(n-1); and the conjugate transpose ' is tighter than the power, so A'^2 means (A')^2. Parenthesise when in doubt.
Concatenation operators
Square brackets [ ] concatenate; curly braces { } build a cell array. Within either form, commas separate row elements and semicolons or newlines separate rows:
[A, B] % horizontal concat
[A; B] % vertical concat
{A, B} % 1×2 cell
Concatenation requires compatible shape along the joining dimension; otherwise the operator raises the well-known “dimensions of arrays being concatenated are not consistent” error. Empty arrays ([]) act as identity for concatenation.
The functions horzcat, vertcat, cat(dim, A, B, ...), and repmat are the function-form equivalents for programmatic use.
Assignment and the LHS
Assignment is =. The left-hand side may be a single variable, a bracketed list (to receive multiple outputs), an indexed expression (to write into part of an array), or a struct or cell access (to set a field or cell):
x = 5;
[U, S, V] = svd(A);
A(2, :) = 0;
s.name = "Ada";
c{1} = [1 2 3];
Assignment is not an expression: it cannot appear inside an arithmetic expression or as a condition. This is a deliberate departure from C and prevents a class of bugs.
Element-wise functions and broadcasting
Almost every function in the standard library is element-wise when applied to an array: sin, cos, exp, log, sqrt, abs, round, floor, ceil, mod, rem. They produce an output of the same shape as the input. A reader learns to trust the element-wise default and is alerted by the rare exceptions — chiefly the reductions sum, prod, mean, max, min, which collapse along a dimension.
The implicit expansion rule extends arithmetic and comparison to arrays of compatible but not identical shape. A + b for a 3×4 A and a 3×1 b adds b to every column of A. The rule is described on its own page.
A note on the : operator vs the range function
The colon’s role as a range constructor (1:5, 0:0.25:1) is overloaded with its role as an index marker (A(:, 1)). The two are distinguishable by context. The colon’s range form is slightly idiomatic to MATLAB: it does not take a count, it takes a step, and the rounded length is determined by the step and the endpoints. The companion function linspace(a, b, n) produces exactly n equally spaced points from a to b and is the right choice when a specific count is wanted.