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 596521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:10:50+00:00 2026-05-13T16:10:50+00:00

This is half a question and half a challenge to the matlab gurus out

  • 0

This is half a question and half a challenge to the matlab gurus out there:
I’d like to have a function take in a logical array (false/true) and give the beginning and ending of all the contiguous regions containing trues, in a struct array.
Something like this:

b = getBounds([1 0 0 1 1 1 0 0 0 1 1 0 0])

should return

b = 3x1 struct array with fields:  
beg   
end

and

>> b(2)

ans = 

   beg: 4

   end: 6

I already have an implementation, but I don’t really know how to deal with struct arrays well so I wanted to ask how you would do it – I have to go through mat2cell and deal, and when I have to deal with much larger struct arrays it becomes cumbersome. Mine looks like this:

df = diff([0 foo 0]);

a = find(df==1); l = numel(a); 
a = mat2cell(a',ones(1,l)) 
[s(1:l).beg] = deal(a{:});

b = (find(df==-1)-1); 
b = mat2cell(b',ones(1,l)) 
[s(1:l).end] = deal(b{:});
  • 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-13T16:10:50+00:00Added an answer on May 13, 2026 at 4:10 pm

    I don’t see why you are using mat2cell, etc. You are making too much of the problem.

    Given a boolean row vector V, find the beginning and end points of all groups of ones in the sequence.

    V = [1 0 0 1 1 1 0 0 0 1 1 0 0];
    

    You get most of it from diff. Thus

    D = diff(V);
    b.beg = 1 + find(D == 1);
    

    This locates the beginning points of all groups of ones, EXCEPT for possibly the first group. So add a simple test.

    if V(1)
      b.beg = [1,b.beg];
    end
    

    Likewise, every group of ones must end before another begins. So just find the end points, again worrying about the last group if it will be missed.

    b.end = find(D == -1);
    if V(end)
      b.end(end+1) = numel(V);
    end
    

    The result is as we expect.

    b
    b = 
        beg: [1 4 10]
        end: [1 6 11]
    

    In fact though, we can do all of this even more easily. A simple solution is to always append a zero to the beginning and end of V, before we do the diff. See how this works.

    D = diff([0,V,0]);
    b.beg = find(D == 1);
    b.end = find(D == -1) - 1;
    

    Again, the result is as expected.

    b
    b = 
        beg: [1 4 10]
        end: [1 6 11]
    

    By the way, I might avoid the use of end here, even as a structure field name. It is a bad habit to get into, using matlab keywords as variable names, even if they are only field names.

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

Sidebar

Related Questions

This is only a half-way programming question. First of all I have a PCI-Express
This might seem like a stupid question I admit. But I'm in a small
I have seen this question asked in a couple of different ways on SO
I've read the papers linked to in this question . I half get it.
For the purposes of this question, I do not have the ability to use
Maybe there's no simple answer to this question, but I ask in case someone
Possible Duplicate: Counting inversions in an array This is an phone interview question :
This is starting to vex me. I recently decided to clear out my FTP,
This is a difficult and open-ended question I know, but I thought I'd throw
This is a self-explanatory question: Why does this thing bubble into my try catch's

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.