Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Finding the longest length of zero's...

Subject: Finding the longest length of zero's...

From: D

Date: 16 Jun, 2010 22:37:04

Message: 1 of 5

Hello,

I have a huge single column set of data, and within this are intervals of zero's, with each interval a different length.

I used find(A==0) , and it returns a vector of the locations of all zero's. I am ultimately interested in finding the starting and stopping indices of the longest sized interval of zero's.

Can anyone provide tips on how to approach this?

Thank you!

Subject: Finding the longest length of zero's...

From: ImageAnalyst

Date: 16 Jun, 2010 22:46:45

Message: 2 of 5

Do you have the Image Processing Toolbox? Is so, it's trivial. Just
call bwlabel and regionprops then pull out the biggest object. It
doesn't matter that it's a 1D "image" instead of a 2D image.

Subject: Finding the longest length of zero's...

From: Matt Fig

Date: 16 Jun, 2010 23:24:22

Message: 3 of 5

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2b1bac7a-a751-4341-b025-2e251f6bbdb7@z25g2000vbk.googlegroups.com>...
> Do you have the Image Processing Toolbox? Is so, it's trivial. Just
> call bwlabel and regionprops then pull out the biggest object. It
> doesn't matter that it's a 1D "image" instead of a 2D image.

If not, you can use your logical result this way:

B = A==0;
st = findstr(B.',[0 1])+1; % If A is a column vector. The starting points
ed = findstr(B.',[1 0]); % The ending points.
% Now, assuming you take care of any boundary problems (I leave to you)....
[idx,idx] = max(ed-st);
idx = st(idx)

Subject: Finding the longest length of zero's...

From: Roger Stafford

Date: 17 Jun, 2010 00:13:03

Message: 4 of 5

"D " <dale.kim@gatech.edu> wrote in message <hvbjmg$303$1@fred.mathworks.com>...
> Hello,
>
> I have a huge single column set of data, and within this are intervals of zero's, with each interval a different length.
>
> I used find(A==0) , and it returns a vector of the locations of all zero's. I am ultimately interested in finding the starting and stopping indices of the longest sized interval of zero's.
>
> Can anyone provide tips on how to approach this?
>
> Thank you!
- - - - - - - - -
  You can also do it this way. Let d be the column vector of data.

 t = diff([false;d==0;false]);
 p = find(t==1);
 q = find(t==-1);
 [maxlen,ix] = max(q-p);
 first = p(ix);
 last = q(ix)-1;

maxlen is the length of the longest consecutive series of zeros, first is the index of its beginning, and last is the index of its end.

Roger Stafford

Subject: Finding the longest length of zero's...

From: D

Date: 17 Jun, 2010 13:48:04

Message: 5 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hvbpaf$hlf$1@fred.mathworks.com>...
> "D " <dale.kim@gatech.edu> wrote in message <hvbjmg$303$1@fred.mathworks.com>...
> > Hello,
> >
> > I have a huge single column set of data, and within this are intervals of zero's, with each interval a different length.
> >
> > I used find(A==0) , and it returns a vector of the locations of all zero's. I am ultimately interested in finding the starting and stopping indices of the longest sized interval of zero's.
> >
> > Can anyone provide tips on how to approach this?
> >
> > Thank you!
> - - - - - - - - -
> You can also do it this way. Let d be the column vector of data.
>
> t = diff([false;d==0;false]);
> p = find(t==1);
> q = find(t==-1);
> [maxlen,ix] = max(q-p);
> first = p(ix);
> last = q(ix)-1;
>
> maxlen is the length of the longest consecutive series of zeros, first is the index of its beginning, and last is the index of its end.
>
> Roger Stafford



Thank you all!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us