MATLAB: Find and edit interval of array when element is equal to a value

arrayconditionintervallenghtMATLABsequence

Hi everyone!
I have a small question.
I have an array that looks something like this [0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0 2 2]
And I'd like to find those intervals of consectuve 2s that are longer than 3 elements. After that I find those interval, I want to cut the first elements untill they reach the length for 3 elements.
So my final array should be like this [0 0 0 0 2 2 2 0 0 0 0 0 0 0 2 2 2 0 0 0 0 2 2]
The first interval of 2s was made by 5 elements, now it's only 3 elements.
The second interval of 2s was made by 7 elements, now it's only 3.
The last one was made by 2 elements, it says the same.
Is it possible to do this?
Thank you very much and I hope you're having a nice day

Best Answer

Here is another approach, which basically relies on an earlier contribution from Jan with a small modifications for your problem. Please also see Jan's earlier contribution at https://www.mathworks.com/matlabcentral/answers/382011-how-to-count-the-number-of-consecutive-identical-elements-in-both-the-directions-in-a-binary-vecto
Maybe this approach is more efficient than utilizing string find, but you would have to do some timing tests to see. For small vectors performance may not be an issue.
x = [0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0 2 2];
% start with idea from jan
% https://www.mathworks.com/matlabcentral/answers/382011-how-to-count-the-number-of-consecutive-identical-elements-in-both-the-directions-in-a-binary-vecto
d = [true, diff(x) ~= 0, true]; % TRUE if values change
n = diff(find(d)) ; % Number of repetitions
% now modify Jan's idea slightly for your application
% the even elements of n are the lengths of the runs of 2's
% clip them so do not exceed 3
n(2:2:end) = min(n(2:2:end),3);
% in preparation for using repelem, build a vector with alternating
% values of zero and 2
v = zeros(size(n));
v(2:2:end) = 2;
% build new vector with maximum run length 3
y = repelem(v, n);