MATLAB: Avoid negative index in array or matrix

negative index arrayoptimizationoptimization loopremove if/else

I'm working on 2 loops in which i have the difference of two indices like this:
for i=1:interval_1
for j=0:interval_2
if(i-j>0)
X(i+1)=c(j+1)*X(i-j);
else
%nothing
end
end
In my situation interval_1 and interval_2 are more or less 1 million so i want to optimize and remove if/else. It would be useful if there exist a command which ignores operations with negative index. Tell me.

Best Answer

Just do this:
for i=1:interval_1
j = min(i-1,i2);
X(i+1)=c(j+1)*X(i-j);
end
Related Question