MATLAB: Calculation of successive diffrence

arraydiffrence

Hi all!
I have a problem with this : I have an array:
% A=[a1 a2 a3 a4 ......an]
i want to calculate succesivelly the diffrenece:
% B=[a2-a1 a3-a2 a4-a3........] but with this condition:
if for exemple a(i)-a(i-1)==2, the diffrence a(i+1)-a(i-1) schould be calculated and the diffrence a(i)-a(i-1)==2 should be eleminated from the Array B.
i wish you could help , Thank you

Best Answer

B = diff(A);
B1 = conv(A,[1 0 -1],'valid');
idx = find(B == 2);
ii = idx <= numel(B1);
i2 = idx(ii);
B(i2) = B1(i2);