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

  • Home
  • SEARCH
  • 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 4231254
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:56:58+00:00 2026-05-21T01:56:58+00:00

I am using Matlab for one of my projects. I am actually stuck at

  • 0

I am using Matlab for one of my projects. I am actually stuck at a point since some time now. Tried searching on google, but, not much success.

I have an array of 0s and 1s. Something like:

A = [0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0];

I want to extract an array of indicies: [x_1, x_2, x_3, x_4, x_5, ..]

Such that x_1 is the index of start of first range of zeros. x_2 is the index of end of first range of zeros.

x_3 is the index of start of second range of zeros. x_4 is the index of end of second range of zeros.

For the above example:

x_1 = 1, x_2 = 3
x_3 = 9, x_4 = 10 

and so on.

Of course, I can do it by writing a simple loop. I am wondering if there is a more elegant (vectorized) way to solve this problem. I was thinking about something like prefix some, but, no luck as of now.

Thanks,

Anil.

  • 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-21T01:56:59+00:00Added an answer on May 21, 2026 at 1:56 am

    If you want to get your results in a single vector like you described above (i.e. x = [x_1 x_2 x_3 x_4 x_5 ...]), then you can perform a second-order difference using the function DIFF and find the points greater than 0:

    x = find(diff([1 A 1],2) > 0);
    

    EDIT:

    The above will work for the case when there are at least 2 zeroes in every string of zeroes. If you will have single zeroes appearing in A, the above can be modified to handle them like so:

    diffA = diff([1 A 1],2);
    [~,x] = find([diffA > 0; diffA == 2]);
    

    In this case, a single zero value will create repeated indices in x (i.e. if A starts with a single zero, then x(1) and x(2) will both be 1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Matlab to do one of my projects. I am stuck at
When using java from Matlab, is there some way to figure out from where
When I´m using MATLAB, sometimes I feel the need to make comments on some
I'm using Git to track some matlab code. A toy example best illustrates the
I am using Matlab's GUIDE for the first time, I am trying to edit
Suppose I'm using the MATLAB IDE and happen to have some very large objects
I like to debug in matlab using commands. some questions: If choose a stack,
Im using matlab and am having some difficulty. I am trying to swap the
I'm using Matlab to take FFTs of signals, and I'm getting stuck on the
I am linking some fortran code (f90) from matlab using mex and I am

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.