Finding islands of zeros in a sequence

后端 未结 6 539
小蘑菇
小蘑菇 2020-11-22 14:05

Imagine you have a very long sequence. What is the most efficient way of finding the intervals where the sequence is all zeros (or more precisely the sequence drops to near-

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 15:08

    You can solve this as a string search task, by finding strings of zeros of length thresh (STRFIND function is very fast)

    startIndex = strfind(sig, zeros(1,thresh));
    

    Note that longer substrings will get marked in multiple locations but will eventually be joined once we add in-between locations from intervals start at startIndex to end at start+thresh-1.

    indices = unique( bsxfun(@plus, startIndex', 0:thresh-1) )';
    

    Note that you can always swap this last step with the CUMSUM/FIND solution by @gnovice from the linked question.

提交回复
热议问题