MATLAB: Repetition of values in a vector.

MATLABvectors

Hello, I have a 24×1 vector containing 0's,1's and 2's. Shorter example being B = [0 2 2 2 2 0 1 2 2 2]'. As you can see there are four 2's repeated after eachother and then three 2's repeated after eachother, is it possible for me to write a code on Matlab that will tell me that there are four 2's repeated directly after eachother but ignore the three 2's repeated. I can't change the order of the vector as its based on hours in the day data. Many Thanks.

Best Answer

out = sum(conv2(B,ones(4,1),'same') == 8);
or
a = conv2(B,ones(4,1),'valid') == 8;
repeats = sum(a == 8);
indexes = find(a);
or
indexes = strfind(B(:)',[2 2 2 2]);
repeats = numel(indexes);
only four 2's
indexes = strfind([0,(B(:)' == 2)+0,0],[0 1 1 1 1 0]);