Table of Contents
Arithmetic in MATLAB
In MATLAB, arithmetic is usually performed on entire arrays at once. Even if you start with single numbers, it is important to understand how these same operators behave when you work with vectors and matrices.
This chapter introduces the basic arithmetic operators, then shows what elementwise operations are and how they differ from matrix operations. You will use these ideas constantly in later chapters.
Basic arithmetic with scalars
A scalar is a single number, such as 3 or -1.5. You can type arithmetic expressions directly in the Command Window and MATLAB immediately evaluates them. The basic operators are:
+ for addition.
- for subtraction and for unary minus.
* for multiplication.
/ for right division.
\ for left division.
^ for exponentiation.
For example:
2 + 5
10 - 3
4 * 7
9 / 2
2^3
-5MATLAB respects the usual precedence rules. Exponentiation is evaluated first, then multiplication and division, then addition and subtraction. Parentheses control the order explicitly. The following three results are different:
2 + 3 * 4 % 2 + (3*4) = 14
(2 + 3) * 4 % 5*4 = 20
2^(3*2) % 2^6 = 64You can also combine scalars with arrays. When you do this, MATLAB usually applies the scalar to each element of the array. This is one of the most useful features of MATLAB and is discussed in more detail below.
Matrix operations versus elementwise operations
MATLAB is built around linear algebra, so many operators have a matrix algebra meaning when both operands are vectors or matrices.
For example, if A and B are matrices of compatible sizes, the operator performs matrix multiplication, not term by term multiplication. In contrast, the operator . performs elementwise multiplication, that is, it multiplies entries in matching positions.
The same idea applies to division and powers. There is a matrix version and an elementwise version of several operators.
Matrix operators:
* matrix multiplication.
/ matrix right division.
\ matrix left division.
^ matrix power.
Elementwise operators:
.* elementwise multiplication.
./ elementwise right division.
.\ elementwise left division.
. ^ elementwise power.
The dot in the elementwise operators is crucial. Without the dot, MATLAB interprets the expression as a matrix operation and expects the sizes of the operands to satisfy matrix algebra rules.
The linear algebra meaning of *, /, \, and ^ is covered in more detail in the linear algebra chapter. Here, the focus is on recognizing when you want elementwise behavior and how to write it.
Elementwise arithmetic with vectors and matrices
Elementwise operations act on corresponding entries of two arrays. The arrays must be the same size, or one of them must be a scalar. In the simplest case, consider two vectors of the same length:
a = [1 2 3];
b = [4 5 6];
c = a + b % [1+4 2+5 3+6]
d = a - b % [1-4 2-5 3-6]
e = a .* b % [1*4 2*5 3*6]
f = a ./ b % [1/4 2/5 3/6]
g = a .^ 2 % [1^2 2^2 3^2]
Addition and subtraction always work elementwise for arrays of the same size, so there is no .+ or .- operator. For multiplication, division, and power, you must use the dot form when you mean elementwise behavior.
The result of an elementwise operation has the same size as the input arrays. If A and B are both 3x3 matrices, then A + B, A .* B, and A .^ 2 are all 3x3.
If you accidentally write A B when you meant A . B, MATLAB tries to do matrix multiplication. If the sizes are not suitable for matrix multiplication, you will get an error. If they are suitable, MATLAB computes something different from what you probably intended.
Using scalars with arrays
A very common pattern is to combine a scalar with an array. In such expressions MATLAB automatically applies the operation elementwise. You do not need a dot when one operand is a scalar.
For example:
A = [1 2 3; 4 5 6];
B = A + 10 % adds 10 to every element
C = 2 * A % multiplies every element by 2
D = A / 10 % divides every element by 10
E = A.^2 % still need dot for power because both operands are arrays
The last example shows an important exception. The power operator has special matrix meanings, even with a scalar exponent. To make sure you get elementwise powers, always use . ^ when the base is an array. Without the dot, A^2 attempts a matrix power, which only makes sense for square matrices and does not square individual entries.
In summary, when you see a scalar combined with an array using +, -, *, /, or \, you get elementwise behavior automatically. For exponentiation with arrays, use . ^.
Combining arithmetic and functions
MATLAB provides many mathematical functions that operate elementwise on arrays by default. These include square root sqrt, absolute value abs, exponential exp, logarithms log and log10, and many others.
When you call such a function with an array input, the function is applied to each element separately, and the result has the same size as the input. Arithmetic operators and functions combine naturally:
x = 0:0.5:5;
y = exp(-x) .* sin(x) + 1;
z = (x.^2 + 3*x + 2) ./ (x + 1);
In the expression for y, the function exp and sin both act elementwise on the vector x. The multiplication is written .* to match the elementwise behavior. In the expression for z, the addition and subtraction are automatically elementwise, while the power and division use . ^ and ./ because x is a vector.
It is common to mix arithmetic and functions in a single expression. The rules are the same: respect operator precedence, use parentheses when needed, and remember to use the dot versions of operators when operating on arrays entry by entry.
Common size and broadcasting behavior
Elementwise operations require that the arrays have compatible sizes. In the simplest case this means they are exactly the same size, or one is a scalar. MATLAB can also automatically expand certain dimensions so that a row vector and a column vector, or a smaller array, can be combined without manual replication. This is sometimes called implicit expansion or broadcasting.
For example:
row = [1 2 3]; % 1x3
col = [10; 20; 30]; % 3x1
M = col + row
In this case MATLAB forms a 3x3 matrix where each element is the sum of one entry from col and one entry from row. The 3x1 and 1x3 sizes are compatible for implicit expansion. Each is conceptually expanded to 3x3 during the calculation.
The same behavior appears in elementwise operations like .*, ./, and . ^ when the sizes are compatible in this way. If sizes are not compatible, MATLAB reports an error describing that the arrays have mismatched dimensions.
You do not need to memorize all the rules, but you should recognize that MATLAB often lets you combine vectors and matrices of different but related sizes without manual loops or replication.
Integer arithmetic and overflow behavior
If you work only with default numeric values, MATLAB uses double precision floating point numbers, which can represent very large and very small values. When you work with integer arrays, arithmetic is still allowed, but the behavior at the limits is different.
With integer types, if an operation would produce a value outside the range of the integer type, MATLAB saturates the result at the nearest representable value. For example, for an unsigned 8 bit integer uint8, the range is from 0 to 255. If you add two uint8 values and the true sum is greater than 255, the stored result is 255.
For instance:
a = uint8(250);
b = uint8(20);
c = a + b % result is 255, not 270Subtraction behaves similarly at the lower bound. If the true result would be negative, it is stored as 0 in an unsigned integer type.
Elementwise operators like .*, ./, and . ^ work with integer arrays but are subject to the same range limitations. If the result of an operation lies outside the integer range, it will saturate at the bounds.
When you need precise mathematical behavior on large ranges, or you plan to work with fractional values, convert integers to double before computations, and convert back afterward if necessary.
Important points to remember:
Always use a dot for elementwise .*, ./, .\, and . ^ when both operands are arrays.
Addition and subtraction of arrays are always elementwise. There is no .+ or .- operator.
Combining a scalar with an array using +, -, *, /, or \ applies the scalar to every element of the array.
Most mathematical functions like sqrt, exp, and sin operate elementwise on arrays and return outputs of the same size.
Integer arithmetic can saturate at the minimum or maximum representable value if results go out of range.