Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 937391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:28:15+00:00 2026-05-15T21:28:15+00:00

Imagine you have a very long sequence. What is the most efficient way of

  • 0

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-zero values abs(X)<eps):

For simplicity, lets assume the following sequence:

sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0];

I’m trying to get the following information:

startIndex   EndIndex    Duration
3            6           4
12           12          1
14           16          3
25           26          2
30           30          1

then using this information, we find the intervals with duration >= to some specified value (say 3), and returning the indices of the values in all these intervals combined:

indices = [3 4 5 6 14 15 16];

That last part is related to a previous question:

MATLAB: vectorized array creation
from a list of start/end indices

This is what I have so far:

sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0];
len = length(sig);
thresh = 3;

%# align the signal with itself successively shifted by one
%# v will thus contain 1 in the starting locations of the zero interval
v = true(1,len-thresh+1);
for i=1:thresh
    v = v & ( sig(i:len-thresh+i) == 0 );
end

%# extend the 1's till the end of the intervals
for i=1:thresh-1
    v(find(v)+1) = true;
end

%# get the final indices
v = find(v);

I’m looking to vectorize/optimize the code, but I’m open to other solutions.
I have to stress that space and time efficiencies are very important, since I’m processing a large number of long bio-signals.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:28:16+00:00Added an answer on May 15, 2026 at 9:28 pm

    These are the steps I would take to solve your problem in a vectorized way, starting with a given vector sig:

    • First, threshold the vector to get a vector tsig of zeros and ones (zeroes where the absolute value of the signal drops close enough to zero, ones elsewhere):

      tsig = (abs(sig) >= eps);  %# Using eps as the threshold
      
    • Next, find the starting indices, ending indices, and duration of each string of zeroes using the functions DIFF and FIND:

      dsig = diff([1 tsig 1]);
      startIndex = find(dsig < 0);
      endIndex = find(dsig > 0)-1;
      duration = endIndex-startIndex+1;
      
    • Then, find the strings of zeroes with a duration greater than or equal to some value (such as 3, from your example):

      stringIndex = (duration >= 3);
      startIndex = startIndex(stringIndex);
      endIndex = endIndex(stringIndex);
      
    • Finally, use the method from my answer to the linked question to generate your final set of indices:

      indices = zeros(1,max(endIndex)+1);
      indices(startIndex) = 1;
      indices(endIndex+1) = indices(endIndex+1)-1;
      indices = find(cumsum(indices));
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET
imagine I have a bunch of C++ related classes (all extending the same base
I have a script that is very long to execute, so when i run
I have a very busy UI, a lot of constant updating without interaction (imagine
Tricky question. I have a very long form where the user writes down a
Imagine you have an entity that has some relations with other entities and you
Imagine I have a table which stores a series of sparse vectors. A sparse
Imagine I have a method: void Method(bool parameter){ if(parameter){ // first case } else
Imagine you have a large dataset that may or may not be filtered by
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.