MATLAB: Longest segment of successive numbers of a sequence

MATLABmatlab functionvector

If I have a vector
[ 1 2 7 8 3 9 4 7 2 5 7 8 9]
I want to find the longest segment of 'successive' numbers in this sequence.
In this example, the sequences would be:
[1 2 7 8], [3 9], [4 7], [2 5 7 8 9]
So the answer in this case will be
[2 5 7 8 9]
How can I do this using inbuilt matlab functions, and no for loops?

Best Answer

>> V = [1,2,7,8,3,9,4,7,2,5,7,8,9];
>> D = [0,find([diff(V)<0,true])];
>> [~,X] = max(diff(D));
>> V(D(X)+1:D(X+1))
ans =
2 5 7 8 9