Saying it once for the whole array
In most languages, computing the squares of a thousand numbers means writing a loop. In MATLAB you write x.^2, and the language applies the operation to every element. This style, called vectorisation, is more than a shortcut. It makes code shorter and closer to the mathematics, it removes a whole class of off-by-one bugs, and it lets the system hand the work to optimised libraries that use every trick the processor has. For a long time vectorised MATLAB code was dramatically faster than loops; modern MATLAB has narrowed the gap with its JIT compiler, but in Octave, which interprets loops more slowly, the difference is still large.
There is one catch, and it trips up everyone at first: because the language is built on linear algebra, * means matrix multiplication, not element-by-element multiplication. Learning which operator you want is the first step. After that come the tools for summarising arrays (sums, means, maxima along rows or columns), the realities of floating-point arithmetic, and the ordinary control flow you know from other languages, for the times when a loop really is the clearest code.
Element-wise and matrix operators
Definition— Element-wise operators
.*, ./ and .^ operate on corresponding elements: [1 2 3] .* [4 5 6] is [4 10 18]. The operands must have compatible sizes (the same size, or sizes that implicit expansion can match). + and - are always element-wise and need no dot.
Definition— Matrix operators
* is matrix multiplication: an m-by-n matrix times an n-by-p matrix gives an m-by-p matrix whose elements are dot products of rows with columns. ^ is a matrix power (A^2 is A*A), and / and \ solve linear systems (lesson 6). With a scalar on either side, * and / act element-wise anyway, so 2 * x and x / 10 need no dot.
a = [1 2 3];
b = [4 5 6];
disp(a + b)
disp(a .* b)
disp(a ./ b)
disp(a .^ 2)
disp(a * b')
disp(a' * b)
A = [1 2; 3 4];
disp(A * A)
disp(A .* A) 5 7 9
4 10 18
0.2500 0.4000 0.5000
1 4 9
32
4 5 6
8 10 12
12 15 18
7 10
15 22
1 4
9 16a * b' multiplies a 1-by-3 row by a 3-by-1 column, giving a 1-by-1 result: the dot product 1·4 + 2·5 + 3·6 = 32. a' * b, a column times a row, gives a 3-by-3 outer product. And A * A is the matrix product, quite different from A .* A, which squares each element. Writing a * b without a transpose would be an error, since a 1-by-3 matrix cannot multiply another 1-by-3 matrix; the error message (operator *: nonconformant arguments (op1 is 1x3, op2 is 1x3) in Octave) is the most common one beginners see, and it nearly always means you wanted .*.
Implicit expansion and reductions
Definition— Implicit expansion (broadcasting)
When an element-wise operation combines arrays of different sizes, each dimension must either match or be 1 in one of them. A dimension of size 1 is stretched to match the other. So a 4-by-3 matrix minus a 1-by-3 row subtracts the row from every row, and a 4-by-1 column times a 1-by-5 row produces a 4-by-5 table. MATLAB has done this since release R2016b; Octave has done it for longer, calling it automatic broadcasting.
Definition— Reduction along a dimension
Functions such as sum, mean, max, min, prod, any and all collapse one dimension of an array. By default they work down the columns (dimension 1), giving one result per column; pass 2 to work along rows. sum(A(:)) sums every element.
Here are four students' marks on three tests:
% Rows are students, columns are three tests.
scores = [70 85 90
60 75 80
95 92 88
50 65 70];
disp(mean(scores))
disp(mean(scores, 2)')
disp(max(scores, [], 2)')
centred = scores - mean(scores);
disp(centred)
weights = [0.2 0.3 0.5];
final = scores * weights';
disp(final')
[best, who] = max(final);
fprintf('best final mark %.1f, by student %d\n', best, who);
fprintf('%d students passed every test\n', sum(all(scores >= 60, 2)));
disp(cumsum([1 2 3 4]))
disp(diff([1 4 9 16 25]))
disp((1:4)' .* (1:5)) 68.750 79.250 82.000
81.667 71.667 91.667 61.667
90 80 95 70
1.2500 5.7500 8.0000
-8.7500 -4.2500 -2.0000
26.2500 12.7500 6.0000
-18.7500 -14.2500 -12.0000
84.500 74.500 90.600 64.500
best final mark 90.6, by student 3
3 students passed every test
1 3 6 10
3 5 7 9
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20Read it as a data analyst would:
mean(scores)gives each test's average (down the columns);mean(scores, 2)gives each student's average (along the rows). Transposing the result with'just prints it as a row.max(scores, [], 2)is each student's best mark. The empty[]is a placeholder:max(a, b)with two arrays means the element-wise maximum, so the dimension goes in the third position.scores - mean(scores)uses implicit expansion: the 1-by-3 row of test averages is subtracted from each of the four rows, centring each test around zero. This is a one-line version of a step that appears in statistics and machine learning constantly.scores * weights'is a matrix-vector product that computes a weighted final mark for every student at once (20 % for the first test, 30 % and 50 % for the others).maxwith two outputs returns the value and its position.all(scores >= 60, 2)asks, for each student, whether every test passed;sumof a logical array counts the trues.cumsumgives running totals anddiffthe differences between neighbours; they are inverse operations, like integral and derivative.(1:4)' .* (1:5)is a 4-by-1 column times a 1-by-5 row, expanded into a multiplication table.
Floating point, Inf, NaN and integer types
fprintf('%.17g\n', 0.1 + 0.2);
disp(0.1 + 0.2 == 0.3)
disp(abs((0.1 + 0.2) - 0.3) < 1e-12)
fprintf('%g\n', eps);
disp([1/0, -1/0, 0/0])
x = [1 NaN 3];
disp(sum(x))
disp(sum(x(~isnan(x))))
disp(NaN == NaN)
disp(int8(100) + int8(100))
disp(uint8(5) - uint8(10))
disp(int32(7) / int32(2))
disp(class(int8(1) * 2.6))
disp(mod(-7, 3))
disp(rem(-7, 3))0.30000000000000004
0
1
2.22045e-16
Inf -Inf NaN
NaN
4
0
127
0
4
int8
2
-1- Doubles are binary floating-point numbers with about 16 significant digits.
0.1 + 0.2is not exactly0.3, so testing them with==gives false (0). Compare floating-point results with a tolerance, as in the third line.epsis the gap between 1 and the next larger double, a useful yardstick for tolerances. - Division by zero does not stop the program: it gives
Inf,-InforNaN("not a number"). A singleNaNpoisons a sum, so remove or ignore missing values withisnan(MATLAB'ssum(x, 'omitnan')and similar options do the same; Octave supports the'omitnan'flag for some functions, such asmean).NaNis not equal even to itself. - Integer types saturate:
int8holds -128 to 127, so 100 + 100 gives 127, and unsigned5 - 10gives 0, with no error. Integer division rounds to the nearest integer rather than truncating, soint32(7) / int32(2)is 4. Mixing an integer type with a double gives the integer type. These rules surprise people coming from C; they exist because integer arrays mostly hold image and sensor data, where clipping at the limits is the right behaviour. modandremdiffer for negative numbers:mod(-7, 3)is 2 (the result has the sign of the divisor),rem(-7, 3)is -1 (the sign of the dividend).
Control flow
MATLAB's control flow is conventional. Every block ends with end:
for n = [3 10 15 22]
if mod(n, 15) == 0
label = 'FizzBuzz';
elseif mod(n, 3) == 0
label = 'Fizz';
elseif mod(n, 5) == 0
label = 'Buzz';
else
label = num2str(n);
end
fprintf('%d -> %s\n', n, label);
end
% Newton's method for the square root of 2
x = 1;
steps = 0;
while abs(x^2 - 2) > 1e-12
x = x - (x^2 - 2) / (2 * x);
steps = steps + 1;
end
fprintf('sqrt(2) is about %.12f after %d steps\n', x, steps);
day = 'Sun';
switch day
case {'Sat', 'Sun'}
disp('weekend')
otherwise
disp('weekday')
end
M = [1 2 3; 4 5 6];
for column = M
fprintf('column sum %d\n', sum(column));
end
n = 100;
while true
n = n + 1;
if mod(n, 2) == 0
continue
end
if isprime(n)
break
end
end
fprintf('first prime above 100: %d\n', n);3 -> Fizz
10 -> Buzz
15 -> FizzBuzz
22 -> 22
sqrt(2) is about 1.414213562373 after 5 steps
weekend
column sum 5
column sum 7
column sum 9
first prime above 100: 101forloops over the columns of whatever follows the=. Over a row vector, that means one element at a time. Over a matrix, the loop variable is a whole column, as thecolumn sumlines show. Over a column vector, the loop runs once with the whole column, a classic surprise: writefor v = x(:)'to be safe.whilerepeats while its condition is true. Newton's method converges very fast: five steps to twelve correct decimals.switchcompares a value with eachcase. It works with numbers and with text (where==would fail on texts of different lengths), and a case can list several alternatives in braces.breakleaves the innermost loop;continueskips to its next iteration.- In conditions, use
&&and||, which stop evaluating as soon as the answer is known. Anifon an array is true only if all its elements are nonzero, which is rarely what you meant; sayany(...)orall(...)explicitly.
Loop or vectorise?
n = 100000;
s = zeros(1, n);
for k = 1:n
s(k) = 1 / k^2;
end
loopSum = sum(s);
vectorSum = sum(1 ./ (1:n).^2);
fprintf('loop: %.10f\n', loopSum);
fprintf('vectorised: %.10f\n', vectorSum);
fprintf('pi^2/6: %.10f\n', pi^2 / 6);loop: 1.6449240669
vectorised: 1.6449240669
pi^2/6: 1.6449340668Both compute the first hundred thousand terms of the series 1 + 1/4 + 1/9 + …, whose infinite sum is π²/6. The partial sums fall short of it by about 0.00001, because the terms left out add up to roughly 1/n. The loop preallocates s with zeros, which matters: without it, the array would grow at every step. The vectorised line is shorter and, in Octave, many times faster. Vectorise when the computation is naturally "the same operation on every element"; keep a loop when each step depends on the previous one, as in Newton's method or a simulation, where vectorising would only obscure the code.
Worked example: a distance matrix
Implicit expansion shines when a computation involves every pair of items. Suppose a courier has five stops on a city grid and wants, for each stop, the nearest other stop. That needs the distance between every pair: a 5-by-5 distance matrix. Here it is computed twice, with loops and with one vectorised line, and then used:
% Positions of five delivery stops on a city grid, in kilometres.
x = [0 3 4 1 6];
y = [0 4 0 2 5];
% The loop version: every pair of stops.
n = numel(x);
Dloop = zeros(n);
for i = 1:n
for j = 1:n
Dloop(i, j) = sqrt((x(i) - x(j))^2 + (y(i) - y(j))^2);
end
end
% The vectorised version: a column minus a row expands to an n-by-n matrix.
D = sqrt((x' - x).^2 + (y' - y).^2);
fprintf('largest difference between the two: %g\n', max(abs(D(:) - Dloop(:))));
disp(round(D * 100) / 100) % MATLAB can also write round(D, 2)
D(1:n+1:end) = Inf; % ignore each stop's distance to itself
[nearest, k] = min(D, [], 2);
for i = 1:n
fprintf('stop %d: nearest is stop %d, %.2f km away\n', i, k(i), nearest(i));
endlargest difference between the two: 0
0 5.0000 4.0000 2.2400 7.8100
5.0000 0 4.1200 2.8300 3.1600
4.0000 4.1200 0 3.6100 5.3900
2.2400 2.8300 3.6100 0 5.8300
7.8100 3.1600 5.3900 5.8300 0
stop 1: nearest is stop 4, 2.24 km away
stop 2: nearest is stop 4, 2.83 km away
stop 3: nearest is stop 4, 3.61 km away
stop 4: nearest is stop 1, 2.24 km away
stop 5: nearest is stop 2, 3.16 km awayThe key line is D = sqrt((x' - x).^2 + (y' - y).^2). x' is a 5-by-1 column and x a 1-by-5 row, so x' - x expands to a 5-by-5 matrix whose element (i, j) is x(i) - x(j), exactly the difference the inner loop computes. Squaring, adding and taking the square root element-wise gives every distance at once, and the two versions agree exactly. Mentally checking sizes like this (column minus row gives a matrix) is the core skill of vectorising.
Two more idioms appear. D(1:n+1:end) = Inf uses linear indexing with a step of n + 1, which walks down the diagonal of an n-by-n matrix (positions 1, 7, 13, … for n = 5), so each stop's zero distance to itself is replaced by infinity and cannot be chosen as its own nearest neighbour. And min(D, [], 2) finds the minimum of each row together with its column, which is the nearest stop.
round(D * 100) / 100 rounds to two decimals for display. MATLAB's round also accepts the number of digits, round(D, 2), but Octave 11's round does not, another small difference worth knowing. For large point sets, the n-by-n matrix grows quickly (a million points would need terabytes), and specialised functions or algorithms that avoid computing every pair take over; for hundreds or a few thousand points, this one-liner is exactly what practitioners write.
Common misconceptions
Misconception
"x * y multiplies two vectors element by element." That is x .* y. * is the matrix product, and for two rows of the same length it fails with a "nonconformant arguments" error; for a column and a row it silently produces a matrix.
Misconception
"Implicit expansion only happens when I ask for it." It happens whenever sizes allow. Adding a row vector to a column vector you thought was a row produces a full matrix without any error. When a result is unexpectedly large, check whether one of your vectors is the wrong orientation.
Misconception
"sum(A) adds up everything." For a matrix it returns one sum per column. Use sum(A(:)) for the total; recent MATLAB and Octave versions also accept sum(A, 'all').
Misconception
"Loops are always slow, so never write one." Loops with preallocated arrays are perfectly reasonable when each step depends on the last, and modern MATLAB runs them quickly. Forcing such code into a vectorised form often makes it slower to read and no faster to run.
Exercises
1. Vectorised arithmetic and control flow
Warm-upComputationalWhat numbers does disp([1 2 3] .* [2 2 2] + 1) show? Write them separated by single spaces.
2. Vectorised arithmetic and control flow
CoreComputationalWhat is [2 0 1] * [3; 4; 5]?
3. Vectorised arithmetic and control flow
CoreComputationalWhat does disp(mat2str(size([1; 2; 3] + [10 20]))) show?
4. Vectorised arithmetic and control flow
Warm-upComputationalWhat does disp(sum([1 2; 3 4])) show? Write the numbers separated by spaces.
5. Vectorised arithmetic and control flow
CoreComputationalWhat is int8(-100) - int8(100)?
6. Vectorised arithmetic and control flow
CoreComputationalWhat is uint8(7) / uint8(2)?
7. Vectorised arithmetic and control flow
StretchComputationalWhat does this display?
count = 0;
for v = [1 2; 3 4]
count = count + 1;
end
disp(count)
8. Vectorised arithmetic and control flow
CoreComputationalWhat does this display?
x = 0;
for k = 1:10
if mod(k, 4) == 0
continue
end
if k > 7
break
end
x = x + k;
end
disp(x)
Further reading and resources
- GNU Octave manual: BroadcastingOctave's description of implicit expansion, which MATLAB also supports.
- GNU Octave manual: Statementsif, switch, while, do-until, for, break and continue.
- GNU Octave manual: Basic Vectorization
- GNU Octave manual: Numeric Data Types