MATLAB® is optimized for operations involving matrices and vectors. The process of revising loop-based, scalar-oriented code to use MATLAB matrix and vector operations is called vectorization. Vectorizing your code is worthwhile for several reasons:
Appearance: Vectorized mathematical code appears more like the mathematical expressions found in textbooks, making the code easier to understand.
Less Error Prone: Without loops, vectorized code is often shorter. Fewer lines of code mean fewer opportunities to introduce programming errors.
Performance: Vectorized code often runs much faster than the corresponding code containing loops.
Vectorizing Code for General Computing
This code computes the sine of 1,001 values ranging from 0 to 10:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);
end
This is a vectorized version of the same code:
t = 0:.01:10;
y = sin(t);
The second code sample usually executes faster than the first and is a more efficient use of MATLAB. Test execution speed on your system by creating scripts that contain the code shown, and then use the tic and toc functions to measure their execution time.
Vectorizing Code for Specific Tasks
This code computes the cumulative sum of a vector at every fifth element:
x = 1:10000;
ylength = (length(x) - mod(length(x),5))/5;
y(1:ylength) = 0;
for n= 5:5:length(x)
y(n/5) = sum(x(1:n));
end
Using vectorization, you can write a much more concise MATLAB process. This code shows one way to accomplish the task:
Vectorization allows you to execute a single instruction on multiple data objects in parallel within a single CPU core, thus improving performance.
This is different from task parallelism using MPI, OpenMP or other parallel libraries where additional cores or nodes are added to take care of data belonging to separate tasks placed on different cores or nodes. This way of parallelism (data parallelism) is possible since modern computer architecture includes SIMD (Single Instruction, Multiple Data) instructions which are able to perform a single instruction on multiple data objects on a single CPU core at the same time.