MATLAB Operators and Expressions
MATLAB supports the standard arithmetic operators +, -, *, /, and ^, but because every variable is a matrix by default, * performs true matrix multiplication (following linear-algebra rules on dimensions) rather than multiplying corresponding elements — attempting A*B on two matrices whose inner dimensions don't match throws a dimension-mismatch error, which is often the first stumbling block for newcomers expecting elementwise behavior.
Cricket analogy: Expecting * to multiply elementwise when it actually performs matrix multiplication is like a new fan assuming a partnership just adds two batters' scores, when in reality a partnership's value depends on how their strike rotation and running between wickets combine -- the rules aren't simply position-by-position.
Element-wise vs Matrix Operators
To force elementwise behavior, MATLAB provides dot-prefixed operators: .* for elementwise multiplication, ./ for elementwise division, and .^ for elementwise exponentiation — so [1 2 3] .* [4 5 6] yields [4 10 18] (each pair multiplied independently), which is the operation newcomers usually actually want when working with vectors of measurements rather than performing linear algebra.
Cricket analogy: Elementwise multiplication with .* is like multiplying each batter's individual strike rate by their own separate bonus multiplier, one player at a time, rather than combining the whole team's stats through some structural partnership formula.
A = [1 2; 3 4];
B = [5 6; 7 8];
A * B % true matrix multiplication (2x2 result via row-column dot products)
A .* B % elementwise multiplication: [5 12; 21 32]
v = [1 2 3];
w = [4 5 6];
v .* w % [4 10 18]
v ./ w % [0.25 0.4 0.5]
v .^ 2 % [1 4 9]
% Colon operator for ranges
1:2:9 % [1 3 5 7 9] -- start:step:stop&& and || require scalar logical operands and will throw an error if given a full array -- use & and | (or all()/any()) when you need to combine conditions across an entire array.
Relational and Logical Operators
Relational operators (==, ~=, <, <=, >, >=) compare elementwise and return a logical array the same size as the operands, while & and | perform elementwise logical AND/OR across whole arrays; && and ||, by contrast, only work on scalar conditions and short-circuit — meaning the second operand isn't even evaluated if the first already determines the result, which matters when the second operand would otherwise error (like checking ~isempty(x) && x(1) > 0).
Cricket analogy: Short-circuit && is like an umpire not bothering to check the third-umpire replay for a run-out if the batter was already given out for handling the ball on the first appeal -- once the outcome is decided, the second check is skipped entirely.
Operator Precedence and the Colon Operator
MATLAB follows standard mathematical operator precedence — parentheses first, then exponentiation (^), then unary plus/minus, then multiplication/division, then addition/subtraction, then relational operators, then &, then | — and the colon operator start:step:stop (or start:stop for a default step of 1) generates evenly spaced row vectors that are used constantly for indexing and loop ranges, so 1:5 produces [1 2 3 4 5] and 10:-2:0 produces [10 8 6 4 2 0].
Cricket analogy: Operator precedence is like the fixed order of a super-over decision tree -- boundary count is checked before wickets lost, which is checked before the coin toss -- MATLAB resolves ^ before * before + in that same fixed, non-negotiable order.
Unary minus binds tighter than you might expect relative to ^: -2^2 evaluates to -4, not 4, because MATLAB computes 2^2 first and then negates the result -- if you actually want (-2)^2, you must add the parentheses explicitly.
- * performs true matrix multiplication by default; use .*, ./, .^ for elementwise operations.
- Relational operators return logical arrays the same size as their operands.
- &/| work elementwise across whole arrays; &&/|| require scalars and short-circuit evaluation.
- Operator precedence follows standard math rules: parentheses, ^, unary sign, */, +-, relational, &, |.
- The colon operator start:step:stop generates evenly spaced vectors used constantly for indexing and loops.
- -2^2 equals -4 in MATLAB because exponentiation binds tighter than unary minus.
- Mismatched matrix dimensions in * throw an error -- a common first bug for MATLAB newcomers.
Practice what you learned
1. What operation does A * B perform when A and B are matrices?
2. What does the expression [1 2 3] .* [4 5 6] evaluate to?
3. What is the key behavioral difference between && and & in MATLAB?
4. What does the expression -2^2 evaluate to in MATLAB?
5. What does 1:2:9 produce?
Was this page helpful?
You May Also Like
MATLAB Variables and Data Types
How MATLAB creates and stores variables, and the core numeric, character, logical, and container data types.
Your First MATLAB Script
How to create, save, and run a MATLAB script file, and the basic building blocks -- comments, output, and simple control flow -- used inside one.
What Is MATLAB?
An introduction to MATLAB as a numerical computing environment and programming language, covering what it's used for and how it differs from general-purpose languages.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics