File Exchange

image thumbnail

findRuns.m

version 1.1.0.0 (549 Bytes) by Richard Heitz
Returns indices of n consecutive numbers (you specify the number you want)

1 Download

Updated 13 Feb 2009

View Version History

No License

Accepts: a vector of numbers

Returns: the indices of n consecutive numbers of your specification

For instance, if you want to find the first instance of 10 consecutive 1's in a vector,

x = findRuns(vector,10,1)

Code may not be pretty, but it works and is quite fast.

Cite As

Richard Heitz (2020). findRuns.m (https://www.mathworks.com/matlabcentral/fileexchange/22972-findruns-m), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Richard Heitz

I agree, using strfind is a much more simplistic method. However, it fails to find ALL instances meeting a criterion. For instance, givin the vector [0 1 1 1 1 0 0], say you want to find instances of 3 or more 1's in a row.
Using strfind you'll get back [2 3]. Using findRuns, you'll get [2 3 4 5].

Jos (10584)

Before evaluating the algorithm, you can improve the help section and add comments to the code. A proper help section consists of (1) an H1 line, (2) and explanation of the algorithm in plain language with a description of the input and output parameters, (3) and example, and perhaps (4) a See Also line.

As it is, "help finruns" returns nothing really useful, unless one take a closer look at the code.

The approach itself is also the result of translating pseudocode (C?) directly into matlab. Using a logical vector, combined with strfind easily returns the indices: Q = vector == value ; startidx = strfind(Q,[ 0 1]) ; etc ... which has been described many times on the newsgroup.

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!