MATLAB: How to write this without loop

loopmatrixwithout loop.

hi! i wrote this in matlab:
%
r is a mtrix of the size(25*30000)
for l=1:30000
for k=2:25
d(k,l)=r(k,l)-r(k-1,l);
if d(k,l)<-109
r(k,l)=r(k,l)+300;
elseif d(k,l)>180
r(k,l)=r(k,l)-300;
end
end
end
how could write this with minimal loops?
thank you

Best Answer

d = [zeros(1, 30000); diff(r)];
index = (d < -109);
r(index) = r(index) + 300;
index = (d > 180);
r(index) = r(index) - 300;
Or:
d = [zeros(1, 30000); diff(r)];
shift = zeros(size(r));
shift(d < -109) = 300;
shift(d > 180) = -300;
r = r + shift;