Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 45 ยท Vectorization and Performance

Vectorization

Learn vectorization in MATLAB and Octave: replace loops with array operations, use implicit expansion, cumsum and diff tricks, and see why it is faster.

Vectorization means writing an operation on a whole array instead of looping over its elements. Compare y = x.^2 + 1 with a for loop that visits each element: the vectorized line is shorter, closer to the mathematics, and much faster, because the loop runs inside optimized compiled code rather than in the interpreter.

Thinking in whole arrays is the habit that separates beginners from fluent MATLAB and Octave users. This lesson shows the main techniques. MATLAB code is read-only here; the TryIts are NumPy stand-ins that make the same point honestly: array operations beat Python-level loops in the same way.

Loop versus vector

Loop One element per interpreter step.
Vector One call, compiled code does the work.

Same result, usually far less time and far less code.

1 / 2

From a loop to one line

Say you want the squares of 1 to 1000 plus one. The loop version:

MATLAB
x = 1:1000;
y = zeros(size(x));
for k = 1:numel(x)
    y(k) = x(k)^2 + 1;
end

The vectorized version:

MATLAB
x = 1:1000;
y = x.^2 + 1;

Almost every built-in function accepts arrays and works element by element: sin(x), exp(x), sqrt(x), abs(x), mod(x, 3). The operators .*, ./ and .^ do the same for arithmetic, as explained in Arithmetic: Matrix vs Element-wise. Forgetting the dot is the most common vectorization bug: x^2 on a row vector is an error, not a squaring of each element.

Python Loop versus vectorized: same answer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The exact timings depend on the machine, so the program prints comparisons instead. In MATLAB the gap is smaller than in pure Python, because modern MATLAB compiles simple loops just in time, but vectorized code is still usually the clearer and often the faster choice.

Conditions without if: logical masks

A loop with an if inside usually becomes a logical mask, a true/false array that selects or replaces elements (see Logical Indexing and find).

MATLAB
v = [4 -2 7 -9 0 5];

v(v < 0) = 0;                 % clip negatives: 4 0 7 0 0 5
count = sum(v > 3);           % how many exceed 3: 3
total = sum(v(v > 3));        % add only those: 16
labels = (v > 3) * 1 + (v == 0) * 2;   % build a code per element

Sums and counts of true values work because true counts as 1. Functions such as any, all, nnz and find complete the set. When you need two branches, compute both and combine them with a mask, for example y = (x < 0).*(-x) + (x >= 0).*x, which is abs(x) written long-hand.

Running totals and neighbor differences

Two functions replace many loops that carry a value from one iteration to the next:

  • cumsum(x) is the running total, cumprod(x) the running product.
  • diff(x) gives x(2:end) - x(1:end-1), the change between neighbors, and is one element shorter.
MATLAB
daily = [3 5 2 8 1];
running = cumsum(daily);        % 3 8 10 18 19
change  = diff(running);        % 5 2 8 1   (recovers daily(2:end))

% Moving sum of width 3 using cumsum
c = [0 cumsum(daily)];
moving3 = c(4:end) - c(1:end-3);   % 10 15 11

The moving-sum trick shows the general idea: shift an array by indexing (c(4:end) against c(1:end-3)) and combine the shifted copies in one expression. Shifting with slices is a core vectorization move, since it lets every element see its neighbor without a loop.

Python cumsum, diff and shifted slices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Implicit expansion: tables without nested loops

When two arrays have compatible shapes, MATLAB (since R2016b) and Octave stretch the size-1 dimensions automatically. A column and a row combine into a whole table:

MATLAB
rows = (1:4)';        % 4x1
cols = 1:5;           % 1x5
T = rows .* cols;     % 4x5 multiplication table, no loop

A matrix minus its column means works the same way: A - mean(A) subtracts each column's mean from that column, and A ./ sum(A, 2) divides each row by its own total. Before implicit expansion existed, code used bsxfun(@minus, A, mean(A)) for the same effect; you will still meet bsxfun in older code, and it is safe to replace with the plain operator. NumPy calls this idea broadcasting.

Python Implicit expansion as broadcasting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When not to vectorize

Vectorization is a tool, not a religion. Keep a loop when:

  • each step depends on the previous result in a way no cumsum, filter or similar function expresses, such as a simulation that updates a state;
  • vectorizing would build a huge temporary array that does not fit in memory;
  • the loop is short and the code is clearer that way.

Profile before you rewrite anything; Performance and Memory explains how. Preallocating the output before a loop often gives most of the benefit with no restructuring. For loop syntax, revisit if, switch and Loops.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does y = x.^2 + 1 do when x is a vector?

    The dot makes the power element-wise. Adding the scalar 1 applies to every element too.
  2. 2. Which expression gives the running total of v?

    cumsum returns all partial sums. diff gives neighbor differences, sum returns one total and cumprod multiplies instead of adds.
  3. 3. How do you replace every negative value in v by zero without a loop?

    v < 0 creates a logical mask; indexing with it selects exactly the negative elements, and the assignment sets them to 0.
  4. 4. What is implicit expansion?

    A 4x1 column and a 1x5 row combine into a 4x5 result because each size-1 dimension is virtually repeated. NumPy calls this broadcasting.

Summary

  • Vectorization applies one operation to a whole array: shorter, closer to the math and usually faster than a loop.
  • Use .*, ./, .^ and built-in functions that accept arrays; forgetting the dot is the classic bug.
  • Replace if inside loops with logical masks, and use sum, any, all and nnz on them.
  • cumsum and diff replace loops that carry values from step to step; shifted slices let elements see neighbors.
  • Implicit expansion combines rows and columns into tables; bsxfun is the older spelling.
  • Keep a loop when steps truly depend on each other or memory is tight, and measure before optimizing.

Next, measure and improve speed in Performance and Memory.