Operators and Precedence
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. The reference material consulted while preparing these pages is listed in the section bibliography. |
Written against MATLAB R2026b. The sessions below were run in GNU Octave 11.3.0 (MATLAB code that Octave also accepts) and show Octave’s output. Octave is more lenient than MATLAB with the logical operators on arrays (see Logical operators); where that matters, the MATLAB behaviour is stated and labelled "not run here". The precedence order is quoted from the R2026b page.
An operator is a symbol that stands for a function applied to its operands: a + b is plus(a,b). This page lists the operators of base MATLAB, explains the single distinction that causes the most confusion (array operations versus matrix operations), then the relational and logical operators, the precedence order that decides how an expression with several operators is read, and the special characters that are not operators in the usual sense. How operands of different sizes combine is on Implicit Expansion and Compatible Sizes.
Arithmetic operators and their function forms
| Operator | Meaning | Function form |
|---|---|---|
|
Addition (element-wise). |
|
|
Subtraction (element-wise). |
|
|
Unary minus. |
|
|
Element-wise (array) multiplication. |
|
|
Matrix multiplication. |
|
|
Element-wise right division. |
|
|
Element-wise left division ( |
|
|
Matrix right division, the solution |
|
|
Matrix left division, the solution |
|
|
Element-wise power. |
|
|
Matrix power (a square matrix to a scalar power, or a scalar to a square matrix). |
|
|
Transpose. |
|
|
Complex conjugate transpose. |
|
Because each operator is a function, you can pass the function form where a function is needed (cellfun(@plus, c1, c2)), and a class can define the operator by defining the method (see Properties and Methods).
The page "MATLAB Operators and Special Characters" lists every operator with its function name; it is the authoritative table.
Array operations versus matrix operations
Array operations act element by element and have a dot before the operator: .*, ./, .\, .^, .'. The dot is not needed for + and -, which are the same in both worlds. Matrix operations follow linear algebra: * is the matrix product (the inner dimensions must agree), ^ multiplies a square matrix by itself, and \ and / solve linear systems.
A = [1 2; 3 4]; B = [5 6; 7 8];
A*B
A.*B
A^2
A.^2
2.^[1 2 3]
Octave 11.3.0 output:
ans =
19 22
43 50
ans =
5 12
21 32
ans =
7 10
15 22
ans =
1 4
9 16
ans =
2 4 8
The most frequent mistake is writing * where .* is meant. With two vectors of the same shape, x*y fails and x.*y works; with a row and a column it silently computes an inner or outer product:
C = [1 2 3]*[4 5 6]'
try, [1 2 3]*[4 5 6], catch err, disp(err.message), end
Octave 11.3.0 output:
C = 32
operator *: nonconformant arguments (op1 is 1x3, op2 is 1x3)
In MATLAB the error is Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication. (identifier MATLAB:innerdim; not run here).
Division and the backslash
A\b solves the linear system A*x = b and is the way to do it; it uses an algorithm chosen from the structure of A (triangular, symmetric positive definite, general, rectangular least squares). Do not write inv(A)*b, which is slower and less accurate. b/A solves x*A = b. When A is square and singular, a warning is issued. For the sparse five-point system of the heatflow scenario, the backslash operator is the solver (see Sparse Matrices and Graphs and Linear Algebra).
A = [1 2; 3 4];
A\[5;6]
A/[5 6; 7 8]
Octave 11.3.0 output:
ans =
-4.0000
4.5000
ans =
3 -2
2 -1
Relational operators
==, ~=, <, <=, >, >= compare element by element and return a logical array of the size of the (expanded) operands. Their function forms are eq, ne, lt, le, gt, ge. The comparison of floating-point numbers with == is exact, which is rarely what you want after arithmetic: 0.1 + 0.2 == 0.3 is false. Use a tolerance, or isapprox (R2024b); see Logical Type and Conditions. isequal compares whole arrays. On character data == compares the characters one by one and needs equal lengths (or a scalar); use strcmp or isequal for text (see Strings and Character Arrays). Relational operators on complex numbers compare only the real parts for < and >, while == compares both.
[1 2 3] == [1 5 3]
Octave 11.3.0 output: 1 0 1.
Logical operators
| Operator | Meaning | Function form |
|---|---|---|
|
Element-wise AND. |
|
|
Element-wise OR. |
|
|
Element-wise NOT. |
|
|
Element-wise exclusive OR. |
|
|
Short-circuit AND for scalar conditions. |
(no function form) |
|
Short-circuit OR for scalar conditions. |
(no function form) |
&, |, ~ and xor work on whole arrays and always evaluate both operands. && and || short-circuit: the second operand is not evaluated when the first already decides the result (false && x is false; true || x is true). Their operands must be scalars (or expressions convertible to a logical scalar), which makes them the operators for if and while conditions: if isnumeric(x) && x > 0.
a = [1 0 1]; b = [1 1 0];
a & b, a | b
~[1 0 2]
xor(true,false)
true || error('not evaluated')
Octave 11.3.0 output:
ans =
1 0 0
ans =
1 1 1
ans =
0 1 0
ans = 1
ans = 1
The last statement returns 1 without calling error, because the right operand is skipped.
The "operands must be convertible to logical scalar" error
In MATLAB, [1 2] && 1 raises Operands to the || and && operators must be convertible to logical scalar values. (identifier MATLAB:nonLogicalScalar; not run here). The same error occurs for an empty operand: [] && true. Octave 11.3.0 is more lenient and evaluates both (if [1 2] && 1 shows its body; if [] && true takes the else branch), so code that appears to work in Octave can fail in MATLAB. When you do want an array condition collapsed, say so with all(x) or any(x): if all(x > 0) && numel(x) > 1.
Operator precedence
Operators of the same level are evaluated from left to right. The R2026b documentation orders the levels from highest to lowest:
| Level | Operators |
|---|---|
1 (highest) |
Parentheses |
2 |
Transpose |
3 |
Power, matrix power, with a unary minus, unary plus or logical negation in the exponent ( |
4 |
Unary plus |
5 |
Multiplication |
6 |
Addition |
7 |
Colon operator |
8 |
Less than |
9 |
Element-wise AND |
10 |
Element-wise OR |
11 |
Short-circuit AND |
12 (lowest) |
Short-circuit OR |
The practical consequences are these.
-
Unary minus is below power, so
-2^2is-(2^2) = -4, not4; write(-2)^2for four. Octave 11.3.0 confirms:-2^2gives-4and(-2)^2gives4. -
A sign in an exponent binds to the exponent:
2^-1is0.5(Octave 11.3.0). The documentation warns that chains of exponentiation combined with unary signs or negations in the exponents, such as2^-2^3written without parentheses, will become an error in a future release, so parenthesise them. -
Power is left-to-right, unlike in mathematics:
2^3^2is(2^3)^2 = 64(Octave 11.3.0 shows64), not2^9. -
The colon is below addition, so
1:3+1is1:4while(1:3)+1is2 3 4. -
Relational operators are below the colon, so
1:3 == 1:3compares the vectors. -
The transpose binds tighter than the unary minus:
-[1 2]'is-([1 2]'), a column (-1,-2). -
Mixed
&and|are evaluated by precedence, not left to right (see above).
-2^2
(-2)^2
2^-1
2^3^2
1:3+1
(1:3)+1
-[1 2]'
A = [3 9 5]; B = [2 1 5];
C = A./B.^2
C = (A./B).^2
Octave 11.3.0 output of the first seven statements: -4, 4, 0.5000, 64, the row 1 2 3 4, the row 2 3 4, and the column -1, -2. The last two come from the documentation page and give 0.7500 9.0000 0.2000 and 2.2500 81.0000 1.0000 (not run here).
Special characters
The operators above are only part of MATLAB’s punctuation. The table gives the meaning of the characters that are not covered by the sections above.
| Character | Meaning |
|---|---|
|
Creates a function handle ( |
|
Decimal point ( |
|
The ellipsis |
|
Separates elements in a row, arguments in a call, subscripts in an index, and statements on a line (displaying the result). |
|
Separates rows in a matrix; ends a statement and suppresses its output. |
|
The colon operator ( |
|
Complex conjugate transpose, or the delimiter of a character vector ( |
|
The delimiter of a string ( |
|
At the start of a line, runs the rest of the line as an operating-system command ( |
|
Starts a comment; |
|
Builds a cell array and indexes into one with curly braces ( |
|
Builds arrays, concatenates, holds the output list of a function ( |
|
Groups expressions, encloses function arguments, and indexes arrays. |
|
Logical negation; as an output placeholder it ignores an output ( |
|
Assignment. |
|
A leading plus marks a namespace folder ( |
|
Metaclass of a class name ( |
Pitfalls
-
*versus.*, and^versus.^(a matrix power of a non-square matrix is an error). -
==on floating-point values; use a tolerance orisapprox. -
&&and||with arrays, an error in MATLAB (and silently accepted in Octave); useallorany. -
-2^2is-4, and power is left-to-right. -
a|b&cisa|(b&c). -
1:n+1is1:(n+1): the colon is below addition. -
inv(A)*binstead ofA\b. -
x'on complex data conjugates. -
=for==in a condition.
Where older books differ
Books for MATLAB 5 to 7 give the same operators and list the same precedence levels, with a few differences. They do not mention implicit expansion (R2016b), so they show bsxfun and repmat where + and .* now suffice. They sometimes recommend | and & in if conditions, which short-circuit only in limited cases; || and && are the safer forms. The precedence of power with a unary sign in the exponent was documented separately in later releases (the third level above).