This solution is outdated. To rescore this solution, sign in.
-
1 Comment
Dirk Engel
on 26 Jun 2012
max(diff(find(~zeroOneVector)))-1 returns the longest run of ones in a vector of zeros and ones, but it FAILS IN SEVERAL CASES, e.g.:
- all ones: [1 1 1 1 1] returns empty
- just a single zero: [1 1 0 1 1] returns empty
- all zeros at one end: [1 1 1 0 0] returns 0
- longest run at one end: [1 1 0 1 0] returns the longest run between zeros, here 1
As a workaround for all cases, add zeros to both ends of the vector:
max(diff(find(~[0 zeroOneVector 0])))-1
However, this is not necessary for this solution because the matrix is very sparse.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!