the code calculates for example A(2:end,:)-A(1:end-1,:) many places in it.
For simple cases like this, you should be using the built-in DIFF command,
D=diff(A,1,1); %equivalent to A(2:end,:)-A(1:end-1,:)
That is to say the most of the time Matlab is just doing the matrix indexing
It is not the indexing itself that is slow. It is that the indexing statements on the right hand side result in the creation of temporary arrays. Indexing on the left hand side should not have this problem, e.g., something like,
for i=1:1e4 C(2:end,:)=X-Y; end
You can work around this this by doing right-hand side indexing outside of the loop, so that temporary arrays will be created only once:
tic;
Ctmp=C(2:end,:); Atmp=A(1:end-1,:);
for i=1:1e4 Ctmp=Ctmp-Atmp; end
C(2:end,:)=Ctmp; A(1:end-1,:)=Atmp;
toc
If you have more complicated indexing that varies from iteration to iteration of the loop, you could also try this FEX file, although I haven't used it myself.