There are two main fails in your code. Elaborating Jan's post;
(1) when i=1 and k=1, i.e. first loop, A(i-k) = A(0) fails. The first element is starting with index 1.
(2) Even if you correct (1), you will see the same error when i=length(A), k=1. Because you attempt to an index A(i+k) which is larger than length(A).