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

a + b

Addition (element-wise).

plus(a,b)

a - b

Subtraction (element-wise).

minus(a,b)

-a

Unary minus.

uminus(a)

a .* b

Element-wise (array) multiplication.

times(a,b)

a * b

Matrix multiplication.

mtimes(a,b)

a ./ b

Element-wise right division.

rdivide(a,b)

a .\ b

Element-wise left division (b ./ a).

ldivide(a,b)

a / b

Matrix right division, the solution x of x*b = a.

mrdivide(a,b)

a \ b

Matrix left division, the solution x of a*x = b.

mldivide(a,b)

a .^ b

Element-wise power.

power(a,b)

a ^ b

Matrix power (a square matrix to a scalar power, or a scalar to a square matrix).

mpower(a,b)

a.'

Transpose.

transpose(a)

a'

Complex conjugate transpose.

ctranspose(a)

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

a & b

Element-wise AND.

and(a,b)

a | b

Element-wise OR.

or(a,b)

~a

Element-wise NOT.

not(a)

xor(a,b)

Element-wise exclusive OR.

xor

a && b

Short-circuit AND for scalar conditions.

(no function form)

a || b

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.

& before |

& has higher precedence than |, so 1 | 0 & 0 is 1 | (0 & 0). Octave 11.3.0 shows ans = 1, which would be 0 if the operators were evaluated from left to right. The same holds for && and ||. Parenthesise mixed expressions.

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 .', power .^, complex conjugate transpose ', matrix power ^

3

Power, matrix power, with a unary minus, unary plus or logical negation in the exponent (.^-, .^+, .^~, ^-, ^+, ^~)

4

Unary plus +, unary minus -, logical negation ~

5

Multiplication .*, right division ./, left division .\, matrix multiplication *, matrix right division /, matrix left division \

6

Addition +, subtraction -

7

Colon operator :

8

Less than <, less than or equal <=, greater than >, greater than or equal >=, equal ==, not equal ~=

9

Element-wise AND &

10

Element-wise OR |

11

Short-circuit AND &&

12 (lowest)

Short-circuit OR ||

flowchart TB L1["1: parentheses ( )"] --> L2["2: transpose, power, complex conjugate transpose, matrix power"] L2 --> L3["3: power with a unary sign or negation in the exponent"] L3 --> L4["4: unary plus, unary minus, logical negation"] L4 --> L5["5: multiplication and division, array and matrix"] L5 --> L6["6: addition, subtraction"] L6 --> L7["7: colon operator"] L7 --> L8["8: relational operators"] L8 --> L9["9: element-wise AND"] L9 --> L10["10: element-wise OR"] L10 --> L11["11: short-circuit AND"] L11 --> L12["12: short-circuit OR"]

The practical consequences are these.

  • Unary minus is below power, so -2^2 is -(2^2) = -4, not 4; write (-2)^2 for four. Octave 11.3.0 confirms: -2^2 gives -4 and (-2)^2 gives 4.

  • A sign in an exponent binds to the exponent: 2^-1 is 0.5 (Octave 11.3.0). The documentation warns that chains of exponentiation combined with unary signs or negations in the exponents, such as 2^-2^3 written without parentheses, will become an error in a future release, so parenthesise them.

  • Power is left-to-right, unlike in mathematics: 2^3^2 is (2^3)^2 = 64 (Octave 11.3.0 shows 64), not 2^9.

  • The colon is below addition, so 1:3+1 is 1:4 while (1:3)+1 is 2 3 4.

  • Relational operators are below the colon, so 1:3 == 1:3 compares 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 (@sin) or an anonymous function (@(x) x.^2); in @ClassName folders, a class folder; in obj@Super, a call to a superclass method.

.

Decimal point (3.14); structure field access (s.name); object property or method (obj.prop); the prefix of element-wise operators; a package or namespace separator (heatflow.laplacian).

.. and ...

The ellipsis ... continues a statement on the next line and ignores the rest of the line. A double dot .. is the parent folder in paths (cd ..).

,

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 (1:5), all elements in a dimension (A(:,1)), and A(:) for all elements as a column.

'

Complex conjugate transpose, or the delimiter of a character vector ('text').

"

The delimiter of a string ("text"), since R2017a (confirmed by secondary sources; the current documentation pages do not state the release).

!

At the start of a line, runs the rest of the line as an operating-system command (!dir). Not a logical negation (use ~).

%

Starts a comment; %{ ... %} is a block comment; %% starts a code section.

{ }

Builds a cell array and indexes into one with curly braces (c{2}), producing the contents.

[ ]

Builds arrays, concatenates, holds the output list of a function ([a,b] = f(x)); [] is the empty array and, on the right of an assignment to an index, deletes elements.

( )

Groups expressions, encloses function arguments, and indexes arrays.

~

Logical negation; as an output placeholder it ignores an output ([~, i] = max(x)); as an input placeholder in a function definition it ignores an argument.

=

Assignment. == is comparison; confusing the two inside an if is an error in MATLAB (= there is an assignment).

+ in a folder name

A leading plus marks a namespace folder (+heatflow); "package" is the former name (see Namespaces and Class Folders).

?

Metaclass of a class name (?double).

Pitfalls

  • * versus .*, and ^ versus .^ (a matrix power of a non-square matrix is an error).

  • == on floating-point values; use a tolerance or isapprox.

  • && and || with arrays, an error in MATLAB (and silently accepted in Octave); use all or any.

  • -2^2 is -4, and power is left-to-right.

  • a|b&c is a|(b&c).

  • 1:n+1 is 1:(n+1): the colon is below addition.

  • inv(A)*b instead of A\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).