Vectorization of 4 nested for loops

1 view (last 30 days)
Moritz
Moritz on 2 May 2015
Edited: Moritz on 2 May 2015
Hello everybody,
I'm trying to vectorize some code that is used in a large numeric engineering model. The original version of the code is:
% non-vectorized code
for i = 1:iMax
for j = 1:round(jMax/2)
for k = 1:iMax
for p = 1:jMax
wIncremental = firstMatrix(k,p)*secondMatrix(iMax-i+k,jMax-j+p);
wIncrementSum(i,j) = wIncrementSum(i,j) + wIncremental;
end
end
end
end
Starting from that, I managed to vectorize the code as follows:
% vectorized code
for i = 1:iMax
for j = 1:round(jMax/2)
wIncrement = firstMatrix.*secondMatrix((iMax-i+1):(2*iMax-i),(jMax-j+1):(2*jMax-j));
wIncrementSum(i,j) = sum(sum(wIncrement));
end
end
My question is: Is there a way of vectorizing the second version even more to achieve even shorter runtime? I'm dreaming of a code that runs without any for loops at all. So far I couldn't come up with any solution to this. If not, do you see any other room for improvement?
Thanks for your help and comments

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!