I have a loop of the form
for j=1:n
for k = max(1,j-d_u):min(j-u-1,m)
f(j, k)
end
end
and it turns out that computing the limits on k
in each iteration is adding a lot of overhead. Turns out that the loop can be rewritten by shifting the condition from k
to j
, which removes the overhead. I wonder if there is a macro somewhere to do this automatically?
The result that I’m looking for in this case, is
for j = max(d_u + 2, 1):min(m+u, n)
for k = j-d_u:j-u-1
f(j, k)
end
end
for j = max(d_u + 2, 1, m+u+1):n
for k = j-d_u:m
f(j, k)
end
end
for j=1:min(d_u + 1, m+u, n)
for k = 1:j-u-1
f(j, k)
end
end
for j=max(1, m+u+1):min(d_u + 1, n)
for k = 1:m
f(j, k)
end
end