MATLAB: Uninterrupted segment length

uninterrupted segments

Let's say I have an array indexes:
Indexes = [1122221122111211112222222]
How do I get the length of uninterrupted 2s? I want to output to be
output=[ 4 2 1 7]

Best Answer

For a number that has longer digits, I am not sure. However, if it is within acceptable number of digits
format long
Indexes = [112222112211121];
newIndexes=str2double(regexp(num2str(Indexes),'\d','match'))
coffee = find(diff([-1 newIndexes -1]) ~= 0);
length = diff(coffee);
output = length(1+(newIndexes(1)==1):2:end);
The output is:
output =
4 2 1