MATLAB: Am I getting the error “Array indices must be positive integers or logical values”

arrayerrorMATLAB

I have the following code. I am unsure as to why I am recieving this error when the array indices is always positive as far as I can tell.
function[f] = ntdd(x,f)
n = length(f);
for i = 2:n
for j = n:-1:1
f(j) = ((f(j)-f(j-1))/(x(j)-x(j-(i+1))));
end
end
end

Best Answer

for i = 2:n
for j = n:-1:1
f(j) = ((f(j)-f(j-1))/(x(j)-x(j-(i+1))));
end
end
looks like when j is 1, you have a f(j-1) which gives you f(0)