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.
Richard Heitz (2020). findRuns.m (https://www.mathworks.com/matlabcentral/fileexchange/22972-findruns-m), MATLAB Central File Exchange. Retrieved .
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].
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.
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!