Skip to main content
v2026.11,986 entries · CC-BY 4.0

MATLAB Operations: Array, Matrix, Relational and Logical Operators

MATLAB operations explained: array vs matrix operators, relational and logical operators and precedence, with worked results from MathWorks docs.

Written and maintained by CASRAI Editorial Board

Published

On this page

MATLAB operations fall into four groups: arithmetic operators (which come in an element-by-element “array” form and a linear-algebra “matrix” form), relational operators that compare values, logical operators that combine true/false results, and a fixed precedence order that decides which runs first. The single habit that prevents most mistakes is knowing whether an operator carries a period: A.*B multiplies matching elements, while A*B performs a matrix product.

Ask CASRAI · free to tryQuestion about this guide? Ask CASRAI answers with linked sources. 2 free questions a day, no account.

Ask your first question freeSubscribe

What MATLAB operations are used for in research

Analysts use operators to apply a formula to a whole data set at once: scaling a column of measurements, subtracting a baseline from every sample, testing which readings exceed a threshold, or solving a linear system. Because MATLAB treats variables as arrays, one expression such as x.^2 replaces a loop. This page is an overview of the operator families; for the detail of single operations see the CASRAI guides on multiplying arrays in MATLAB, exponentiation in MATLAB, matrix multiplication in MATLAB and transposing in MATLAB.

Install or access (official source only)

MATLAB is a MathWorks product, and its operator documentation is published at mathworks.com/help/matlab. Obtain the software through your institution’s MathWorks account or the MathWorks website, and use the documentation that matches your release. Every operator described below is a core language feature, so no toolbox is needed to use them.

Step-by-step: choose and apply the right operator

  1. Decide whether you want element-by-element or linear-algebra behaviour. MathWorks defines array operations as working “element by element” and supporting multidimensional arrays; matrix operations “follow the rules of linear algebra” and are not intended for multidimensional arrays.
  2. Add the period for element-by-element work. The array forms are .*, ./, ., .^ and .'. Addition and subtraction (+, -) behave identically in both families, so .+ and .- are not needed.
  3. Use the plain form for linear algebra. * is the matrix product (columns of the first matrix must equal rows of the second), solves Ax = B, / solves xA = B, ^ raises a matrix to a scalar power and ' is the conjugate transpose. The documentation also notes the identity B/A = (A'B')'.
  4. Check the sizes. Inputs of the same size combine by position. Inputs whose sizes are compatible (each dimension is equal, or one of them is 1) are expanded automatically, a behaviour MathWorks calls implicit expansion. Incompatible sizes produce an error.
  5. Compare with relational operators. ==, ~=, >, >=, < and <= return logical results. A single = assigns; == compares the elements of two arrays.
  6. Combine results with logical operators. &, | and ~ work on arrays; && and || are the short-circuit forms.
  7. Add parentheses wherever order matters. The next sections show why.

Output and how to read it

Take A = [1 3; 2 4] and B = [3 0; 1 5], the example MathWorks uses. A*B returns [6 15; 10 20], because each entry is a row-by-column sum. A.*B returns [3 0; 2 20], because each entry is simply the product of the two entries in the same position. Both are valid; they answer different questions, which is why the period matters. The data table below sets these and other documented examples side by side.

Implicit expansion examples

  • [1 1 1]+[1 2 3] returns [2 3 4].
  • 3.*[1 2 3; 1 2 3] returns [3 6 9; 3 6 9].
  • With x = [1 2 3] (a row) and y = [10; 15] (a column), x+y returns [11 12 13; 16 17 18].
  • A 3-by-3 matrix A minus a two-element vector m = [2 4] raises an error, because 3 and 2 are neither equal nor 1.

Operator precedence, with a worked example

MathWorks lists precedence from highest to lowest as follows. Operators on the same level are evaluated left to right, and & binds tighter than |.

Level Operators
1 (highest) Parentheses
2 Transpose and power: .', .^, ', ^
3 Power with unary minus, unary plus or logical negation
4 Unary +, -, ~
5 Multiplication and division: .*, ./, ., *, /,
6 Addition and subtraction: +, -
7 Colon operator
8 Relational: <, <=, >, >=, ==, ~=
9 &
10 |
11 &&
12 (lowest) ||

Let A = [3 9 5] and B = [2 1 5]. Because .^ outranks ./, the expression A./B.^2 squares B first and returns [0.75 9.0 0.2]. Writing (A./B).^2 divides first and returns [2.25 81 1]. Same symbols, different research result. MathWorks advises using parentheses, and warns that chaining an exponent with unary operators needs parentheses because the current behaviour is slated to become an error in a future release.

Which operator family fits which task

This decision aid restates the documented rules as questions you can ask of your own expression.

  • Do you want each element treated independently? Use the period forms (.*, ./, .^). They also work on multidimensional arrays, which the matrix forms are not meant for.
  • Are you solving a linear system? Use for Ax = B or / for xA = B. These are matrix operations, not element-by-element division.
  • Do you need conjugation when transposing complex data? ' returns the conjugate transpose; .' transposes without conjugating. For real-valued data the two give the same numbers.
  • Are you filtering or flagging values? Build a logical array with a relational operator, then combine conditions with & or |. Because & outranks |, wrap mixed conditions in parentheses so the intent is explicit.
  • Are you mixing a row and a column? Expect implicit expansion into a two-dimensional result, as in the 2-by-3 example above, rather than an error, as long as each dimension is equal or 1.

Reading the documented examples this way turns the operator list into a checklist you can apply before running a long analysis script.

Troubleshooting

“Incompatible sizes” or a dimension error

The operands failed the compatibility test: some dimension is neither equal nor 1. Check size(A) and size(m), then reshape or transpose one operand so the dimensions line up, as in the row-plus-column example above.

The result is a matrix product when you expected element-wise values

You wrote *, / or ^ where .*, ./ or .^ was intended. For vectors of equal length the plain form either errors or collapses to a single number. Add the period.

A comparison seems to do nothing

Check that you used ==, not =. The single character assigns a value rather than comparing two arrays.

Changed results after editing a formula

Check precedence. The A./B.^2 example shows how one pair of parentheses changes every element of the output. The CASRAI guide to vectorizing MATLAB code covers writing whole-array expressions in a way that avoids this ambiguity.

Free alternatives

The same operator ideas exist in open-source tools. The table records what each tool’s own documentation states about the operators discussed here.

Tool Element-by-element Matrix / linear algebra Size mismatch behaviour
GNU Octave .*, ./, .^ (“element-by-element” multiplication, right division, power) * matrix multiplication, / right division, left division, ^ power Documented on the Octave arithmetic operators page
NumPy (Python) * works element by element on arrays Separate matrix-product tools, not * Broadcasting: dimensions are compared from the rightmost; compatible if equal or one is 1; otherwise a ValueError

The practical difference for MATLAB users moving to NumPy is that the meaning of * flips: in MATLAB it is the matrix product, in NumPy it is element-by-element. For R readers, see the CASRAI guide to matrix multiplication in R for the equivalent contrast.

FAQ

What is the difference between A*B and A.*B in MATLAB?

A*B is the matrix product and requires the column count of A to equal the row count of B. A.*B multiplies corresponding elements. For A = [1 3; 2 4] and B = [3 0; 1 5], they give [6 15; 10 20] and [3 0; 2 20].

What are the relational and logical operators in MATLAB?

Relational: ==, ~=, >, >=, <, <=. Logical: &, |, &&, ||, ~.

Which MATLAB operator runs first?

Parentheses, then transpose and power, then unary operators, then multiplication and division, then addition and subtraction, then the colon, relational, and logical operators in the order shown in the precedence table. Equal-precedence operators run left to right.

Do I need a period for addition?

No. + and - are the same for array and matrix operations.

Sources (official documentation)

Data: Documented MATLAB operator examples: array, matrix and implicit-expansion results

Worked examples drawn from MathWorks documentation pages on array vs. matrix operations and operator precedence, compiled to compare each operator's family, the rule it follows and the documented result.

Documented MATLAB operator examples: array, matrix and implicit-expansion results
ExpressionOperator familyRuleDocumented result
A*B with A=[1 3;2 4], B=[3 0;1 5]MatrixLinear-algebra product[6 15;10 20]
A.*B with the same A and BArrayElement-by-element product[3 0;2 20]
[1 1 1]+[1 2 3]Array (addition)Same size, match by position[2 3 4]
3.*[1 2 3;1 2 3]ArrayScalar expanded to array size[3 6 9;3 6 9]
x+y with x=[1 2 3], y=[10;15]Array (implicit expansion)Row and column expand to 2-by-3[11 12 13;16 17 18]
A-m with A 3-by-3, m=[2 4]Array (subtraction)Incompatible sizesError
A./B.^2 with A=[3 9 5], B=[2 1 5]Array with precedencePower evaluated before division[0.75 9.0 0.2]
(A./B).^2 with the same A and BArray with parenthesesDivision evaluated first[2.25 81 1]

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Operations: Array, Matrix, Relational and Logical Operators

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →