Calculating the cumulative sum repeatedly consumes a lot of time. Using cumsum this can be made more efficiently with tiny differences by round-off in the magnitude of EPS for RAND data. Please check if these rounding differences can be neglected for your data.
k = 100;
d = 1e4;
lambda = 1;
z = rand(d+2,1);
B = zeros(k,d);
A = zeros(k,d);
C = zeros(k,d);
zc = cumsum(z);
for s = 0:d-1
for r = max(k-(d-s),0):min(s,k-1)
A(r+1,s+1) = lambda/(lambda+1)*z(k-r);
C(r+1,s+1) = z(k-r+s+2);
ci = k - r + 2;
cf = k - r + s + 1;
if ci <= cf
B(r+1, s+1) = lambda / ((s+1) * lambda + r + 1) * (zs(cf) - zs(ci-1));
end
end
end
I've renamed "l" (lowercase L) to "s" to avoid the confusion with "1" (one).
This reduces the processing time for k=100, d=1e4 from 30.42 sec to 0.12 sec. Fine!
After the sum is not the bottleneck anymore, other details get important: Computing r+1, s+1 and lambda/(lambda+1) repeatedly wastes time. Create temporary variables in the outermost possible loop (is this still English?) to save further computing time:
t1 = lambda / (lambda + 1);
for s = 0:d-1
s1 = s + 1;
s1lambda = s1 * lambda;
for r = max(k-(d-s),0):min(s,k-1)
r1 = r + 1;
A(r1, s1) = t1 * z(k-r);
if s1 >= 2
B(r1, s1) = lambda / (s1lambda + r1) * (zs(k - r + s1) - zs(k - r + 1));
end
C(r1, s1) = z(k-r+s+2);
end
end
0.09947 sec, more than 300 times faster.
Converting the original algorithm to C would suffer from the repeated summing also. So if you want to spend time in further optimization, start from the cleaned method.
Summary: Avoid repeated calculations of the same data.
[EDITED] A partially vectorized method to create C:
for s = 0:d-1
q = k + s + 2;
m1 = max(k - d + s, 0);
m2 = min(s, k-1);
C(m1+1:m2+1, s+1) = z(q - m1:-1:q - m2);
end
But this is 50% slower than the loop already, because creating the temporary vectors needs a lot of time. Therefore I think that the cleaned loop is remarkably faster than a partially or completely vectorized version.
5 Comments
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_87697
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_87697
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_87698
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_87698
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_88150
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_88150
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_88185
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_88185
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_88568
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/42711-vectorize-double-loops-with-constraints-on-the-indexes#comment_88568
Sign in to comment.