MATLAB: How to delete consecutive values in a vector and to save just the biggest one

consecutivefor loopif statementvector

I have a vector like: a=[1 2 3 7 10 11 12]. The idea that i want to implement is: if I have consecutive numbers (in this situation) 1 2 3 and 10 11 12 to save just the biggest one and still to not delete the "7". So it should become: a=[3 7 12].
My code (it doesnt work properly and gives me error also):
a=[1 2 3 7 10 11 12]
k=numel(a);
for n=2:k
if a(n)==(a(n-1)+1)
a(n)=[];
end
end

Best Answer

If the sequences are integer and increasing only:
>> a = [1,2,3,7,10,11,12];
>> x = [diff(a)~=1,true];
>> a(x)
ans =
3 7 12