Saturday, 17 August 2013

Matlab - Speeding up Nested For-Loops

Matlab - Speeding up Nested For-Loops

I'm working on a function with three nested for loops that is way too slow
for its intended use. The bottleneck is clearly the looping part - almost
100 % of the execution time are spent in the innermost loop.
The function takes a 2d matrix called rM as input and returns a 3d matrix
called ec:
rows = size(rM, 1);
cols = size(rM, 2);
%preallocate.
ec = zeros(rows+1, cols, numRiskLevels);
ec(1, :, :) = 100;
for risk = minRisk:stepRisk:maxRisk;
for c = 1:cols,
for r = 2:rows+1,
ec(r, c, risk) = ec(r-1, c, risk) * (1 + risk * rM(r-1, c));
end
end
end
Any help on speeding up the for loops would be appreciated...

No comments:

Post a Comment