I'm looking for any good tutorials on vectorizing (loops) in MATLAB.
I have quite simple algorithm, but it uses two for
loops. I know that it should be simple to vectorize it and I would like to learn how to do it instead of asking you for the solution.
But to let you know what problem I have, so you would be able to suggest best tutorials that are showing how to solve similar problems, here's the outline of my problem:
B = zeros(size(A)); % //A is a given matrix.
for i=1:size(A,1)
for j=1:size(A,2)
H = ... %// take some surrounding elements of the element at position (i,j) (i.e. using mask 3x3 elements)
B(i,j) = computeSth(H); %// compute something on selected elements and place it in B
end
end
So, I'm NOT asking for the solution. I'm asking for a good tutorials, examples of vectorizing loops in MATLAB. I would like to learn how to do it and do it on my own.
Vectorization is one of the core concepts of MATLAB. With one command it lets you process all elements of an array, avoiding loops and making your code more readable and efficient. For data stored in numerical arrays, most MATLAB functions are inherently vectorized.
MATLAB is designed to perform vector operations really quickly. MATLAB is an interpreted language, which is why loops are so slow in it. MATLAB sidesteps this issue by providing extremely fast (usually written in C, and optimized for the specific architecture) and well tested functions to operate on vectors.
Vectorization is the process of converting an algorithm from operating on a single value at a time to operating on a set of values at one time. Modern CPUs provide direct support for vector operations where a single instruction is applied to multiple data (SIMD).
Here are a couple of MathWorks tutorials I often link to as references on the subject:
And here's one of Loren's blog posts that has a nice walkthrough of code vectorization for a particular sample problem:
The particular type of problem you gave as a sample, which involves processing submatrices of a given matrix, could be vectorized in different ways depending greatly on what sort of operation you are doing. You may be able to use CONV2 or FILTER2 instead of your nested for loops. There are also a number of functions in the Image Processing Toolbox that handle neighborhood and block processing of matrices, such as NLFILTER and BLOCKPROC. The documentation for these functions should help you figure out how to use them as a way to vectorize your code.
There's a little writeup that I did a year ago to explain a trick that I found after having spent 3 years writing Matlab code daily, often spending too much time vectorizing everything.
http://www.gyomalin.com/reasonable_vectorization.html
The main idea is that you can get a long way just by vectorizing your code along one dimension. Some of you might have already discovered that trick, but I think it's worth being called a Matlab design pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With