NOTE: This is the third in my series of “10 things I wish I had known when I started using MATLAB”.

MATLAB is set up to run very efficiently on what is called vectorized code. What that means is that it carries out computations in a matrix- or vector-based way.

Practically, what this means is that it is often more efficient to replace a for loop with its vectorized equivalent. Maybe an example will help.

In a previous post, we calculated the value of $e^{0.2}$ using the Taylor series expansion:

lim = 5000;   % # of terms in expansion
x = 0.2;   % value of exponent
taylor = 0;

tic
for i = 0:lim
   taylor = taylor + x^i/factorial(i);   % add term to Taylor expansion
end
toc

As implemented with this for loop, this calculation takes 12 milliseconds on my computer (read the post on the tic() and toc() functions).

Now, what if we vectorize this to get rid of the for loop?

tic
i = 0:5000;
taylor = sum(x.^i./factorial(i));
toc

It takes a measly 0.8 milliseconds! Over an order of magnitude improvement in efficiency just by vectorizing!!!

Not only do we get an improvement in speed, but it takes one less line.


Read more at the official MathWorks documentation about vectorization.