Operators in MATLAB
Learn MATLAB operators including arithmetic, comparison, logical, and the critical distinction between matrix and element-wise operations
Operators are the building blocks of every MATLAB expression. Because MATLAB was designed around the matrix as its fundamental data type, its operators carry a distinction that most other languages lack: nearly every arithmetic operator has both a matrix form (*, /, ^) and an element-wise form (.*, ./, .^). Choosing the right one is essential, and getting it wrong is one of the most common sources of bugs for newcomers.
MATLAB’s array-first design means scalar operations automatically extend across entire matrices. Adding a scalar to a matrix adds it to every element. Multiplying two matrices with * performs true matrix multiplication, while .* multiplies element-by-element. This dual model gives MATLAB its expressive power for linear algebra while still supporting ordinary numeric computation.
In this tutorial you’ll learn the arithmetic, relational, and logical operators, how the element-wise variants differ from their matrix counterparts, how comparison produces logical arrays, and how short-circuit logical operators differ from their element-wise siblings.
Arithmetic Operators
MATLAB provides the standard arithmetic operators plus a parallel set of element-wise operators prefixed with a dot.
Create a file named operators_arithmetic.m:
| |
Notice MATLAB has both / (right division) and \ (left division). For scalars a \ b is simply b / a, but for matrices the left-division operator solves the linear system A*x = b — a uniquely MATLAB feature.
Matrix vs. Element-wise Operators
This distinction is the single most important operator concept in MATLAB.
Create a file named operators_matrix.m:
| |
A * B performs the standard linear-algebra dot-product across rows and columns. A .* B produces a matrix where each cell is A(i,j) * B(i,j). The two answers are completely different — the dot prefix is not optional decoration.
Comparison Operators
Comparison operators return logical arrays (matrices of 1 and 0) — not single booleans — when applied to vectors or matrices.
Create a file named operators_compare.m:
| |
MATLAB uses ~= for “not equal” — a holdover from its Fortran-influenced roots. Combined with logical indexing, comparison operators let you filter arrays without writing an explicit loop.
Logical Operators
MATLAB has two flavors of logical operators: element-wise (&, |, ~) which work on arrays, and short-circuit (&&, ||) which work on scalars and stop evaluating once the result is known.
Create a file named operators_logical.m:
| |
Use && / || inside if conditions where short-circuit evaluation prevents errors. Use & / | when combining logical arrays element-by-element.
Assignment, Concatenation, and Precedence
Create a file named operators_misc.m:
| |
A few MATLAB quirks worth highlighting: there are no +=/-= shortcuts (you must repeat the variable), the brackets [ ] double as a concatenation operator, the colon : builds ranges with optional step, and ' is the conjugate transpose while .' is the plain transpose — they differ only for complex numbers.
Running with Docker
| |
Expected Output
Output from operators_arithmetic.m:
a + b = 13
a - b = 7
a * b = 30
a / b = 3.3333
a \ b = 0.3000
a ^ b = 1000
mod = 1
-10
Output from operators_matrix.m:
Matrix multiplication A * B:
19 22
43 50
Element-wise multiplication A .* B:
5 12
21 32
Matrix power A^2 (equals A * A):
7 10
15 22
Element-wise power A.^2 (each element squared):
1 4
9 16
Element-wise division A ./ B:
0.2000 0.3333
0.4286 0.5000
Output from operators_compare.m:
x == y:
0 0 1 0 0
x ~= y:
1 1 0 1 1
x < y:
1 1 0 0 0
x >= y:
0 0 1 1 1
Positive values: 10 7 4
Output from operators_logical.m:
a && b = 0
a || b = 1
~a = 0
v1 & v2:
1 0 0 0
v1 | v2:
1 1 1 0
xor(v1,v2):
0 1 1 0
skipped division by zero
Output from operators_misc.m:
x = 6
Hello, MATLAB!
1 2 3 4 5
0 2 4 6 8 10
10 9 8 7 6
2 + 3 * 4^2 = 50
Transpose M':
1
2
3
Key Concepts
- Matrix vs. element-wise is MATLAB’s signature distinction:
*/^apply linear-algebra rules, while.*./.^operate on corresponding cells. Always think about which one you want. - Comparison produces logical arrays, not single booleans, when operating on vectors or matrices — enabling powerful logical indexing like
data(data > 0). ~=is “not equal” and~is the logical NOT — MATLAB never uses!or!=.- Two logical families:
&&||short-circuit on scalars (safe insideif), while&|work element-wise across arrays. - No compound assignment — write
x = x + 1, neverx += 1. This is one of the most common surprises moving from C-family languages. - The colon
:is an operator that builds ranges (start:step:stop) and selects entire dimensions in indexing (A(:,1)). - Left division
\is unique: for matrices it solvesA*x = bdirectly — a feature no other mainstream language exposes as a built-in operator. - Two transposes:
'conjugates (for complex numbers),.'does not. For real matrices they produce identical results.
Running Today
All examples can be run using Docker:
docker pull gnuoctave/octave:9.4.0
Comments
Loading comments...
Leave a Comment