MATLAB: Identifying and isolating consecutive numbers

consecutive numbersidentify

I have a vector, for example, A= [1 2 3 4 14 15 23 24 25 ]
and I want a code that will identify regions of consecutive numbers and separate them into their own array. ie, a code that will split A into
B = [1 2 3 4] C = [14 15] D = [23 24 25]
I would like this code to be able to work on a matrix A of variable length. Any suggestions?
Thank you!

Best Answer

A(end+1) = 2 % Add a new isolating point end
I_1 = find(D ~= 1); % Find indexes of isolating points
[m,n] = size(I_1);
Start_Idx = 1 ; % Set start index
for i = 1:n
End_Idx = I_1(i); % Set end index
Sequ = A(Start_Idx:End_Idx) % Find consecuative sequences
Start_Idx = End_Idx + 1;
% update start index for the next consecuitive sequence
end