Count the occurrence of consecutive 1s in 0-1 data in MATLAB

Viewed 7683

I have a set of 1s and 0s. How do I count the maximum number of consecutive 1s?

(For example, x = [ 1 1 0 0 1 1 0 0 0 1 0 0 1 1 1 ....]). Here the answer is 3 because the maximum number of times 1 occurs consecutively is 3.

I was looking at some search and count inbuilt function, however I have not been successful.

4 Answers

Cody Problem 15 is find maximum consecutive ones in a 'binary' string. This works quite nicely. As you can tell I'm quite pleased with it! Cody size 19

max(cellfun(@numel,strsplit(x,'0')));
Related