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:
count regions of consecutive zeros in a series

Subject: count regions of consecutive zeros in a series

From: Ignazio G

Date: 10 Nov, 2009 16:18:03

Message: 1 of 12

Hello,
I need to count how many times there are consecutive zeros in a series of 14thousand records.

say I have
Qj = [1.2 5.4 6.2 3 0 0 0 0 2.1 2.3 4.5 6.9 7.2 3.1 0 0 1.1 2.4 5.5]

In this case I would detect 2 times when there were consecutive zeros.

 All I came up with so far was

zeros=sum(Qj == 0)

which only counts the zeros in the series and not the times where there are 2 or more zeros.

thank you in advance

Subject: count regions of consecutive zeros in a series

From: Rick Rosson

Date: 10 Nov, 2009 17:28:48

Message: 2 of 12

Please try the following:

    idx = (Qj(1:end-1) == 0) & (Qj(2:end) ~= 0 );
    N = sum(idx);

This code should work in almost all cases, except if the last element is '0'
while the second-to-last element is not '0'. In that case, N will be one
less than the correct answer.

To account for that case, add the following lines of code:

    if (Qj(end-1) ~= 0) & (Qj(end) == 0)
        N = N + 1;
    end

HTH.

Best,

Rick



"Ignazio G" <igiuntoli@yahoo.it> wrote in message
news:hdc3nq$2hq$1@fred.mathworks.com...
> Hello,
> I need to count how many times there are consecutive zeros in a series of
> 14thousand records.
>
> say I have
> Qj = [1.2 5.4 6.2 3 0 0 0 0 2.1 2.3 4.5 6.9 7.2 3.1 0 0 1.1 2.4 5.5]
>
> In this case I would detect 2 times when there were consecutive zeros.
>
> All I came up with so far was
>
> zeros=sum(Qj == 0)
>
> which only counts the zeros in the series and not the times where there
> are 2 or more zeros.
>
> thank you in advance

Subject: count regions of consecutive zeros in a series

From: Matt Fig

Date: 10 Nov, 2009 17:45:19

Message: 3 of 12

Zero_idx = findstr(Qj,[0 0]);
Zero_idx = Zero_idx(diff([1 Zero_idx])~=1)

Subject: count regions of consecutive zeros in a series

From: Jos

Date: 10 Nov, 2009 17:52:18

Message: 4 of 12

"Ignazio G" <igiuntoli@yahoo.it> wrote in message <hdc3nq$2hq$1@fred.mathworks.com>...
> Hello,
> I need to count how many times there are consecutive zeros in a series of 14thousand records.
>
> say I have
> Qj = [1.2 5.4 6.2 3 0 0 0 0 2.1 2.3 4.5 6.9 7.2 3.1 0 0 1.1 2.4 5.5]
>
> In this case I would detect 2 times when there were consecutive zeros.
>
> All I came up with so far was
>
> zeros=sum(Qj == 0)
>
> which only counts the zeros in the series and not the times where there are 2 or more zeros.
>
> thank you in advance

Do you want to detect the number of times a sequence of one or more zeros are present in your data? If so, then here is one of the many solutions:

Q = [1.2 5.4 6.2 3 0 0 0 0 2.1 2.3 4.5 6.9 7.2 3.1 0 0 1.1 2.4 5.5]
N = numel(regexp(char(double(Qj~=0)+'0'),'0+'))

hth
Jos

Subject: count regions of consecutive zeros in a series

From: Ignazio G

Date: 11 Nov, 2009 00:07:02

Message: 5 of 12

"Jos " <#10584@fileexchange.com> wrote in message <hdc98i$m4l$1@fred.mathworks.com>...
> "Ignazio G" <igiuntoli@yahoo.it> wrote in message <hdc3nq$2hq$1@fred.mathworks.com>...
> > Hello,
> > I need to count how many times there are consecutive zeros in a series of 14thousand records.
> >
> > say I have
> > Qj = [1.2 5.4 6.2 3 0 0 0 0 2.1 2.3 4.5 6.9 7.2 3.1 0 0 1.1 2.4 5.5]
> >
> > In this case I would detect 2 times when there were consecutive zeros.
> >
> > All I came up with so far was
> >
> > zeros=sum(Qj == 0)
> >
> > which only counts the zeros in the series and not the times where there are 2 or more zeros.
> >
> > thank you in advance
>
> Do you want to detect the number of times a sequence of one or more zeros
a sequence of TWO or MORE zeros
>are present in your data? If so, then here is one of the many solutions:
>
> Q = [1.2 5.4 6.2 3 0 0 0 0 2.1 2.3 4.5 6.9 7.2 3.1 0 0 1.1 2.4 5.5]
> N = numel(regexp(char(double(Qj~=0)+'0'),'0+'))
>
> hth
> Jos

How do I modify
N = numel(regexp(char(double(Qj~=0)+'0'),'0+'))
so that it counts just 2 or more zeros in sequence?

Subject: count regions of consecutive zeros in a series

From: Jos

Date: 11 Nov, 2009 18:41:05

Message: 6 of 12

"Ignazio G" <igiuntoli@yahoo.it> wrote in message <hdcv76$qu4$1@fred.mathworks.com>...

* snip *

> How do I modify
> N = numel(regexp(char(double(Qj~=0)+'0'),'0+'))
> so that it counts just 2 or more zeros in sequence?

numel(regexp(char(double(Qj~=0)+'0'),'00+'))

Jos

Subject: count regions of consecutive zeros in a series

From: Matt Fig

Date: 11 Nov, 2009 19:04:05

Message: 7 of 12

I would say that one should avoid regexp on numeric data unless it is unavoidable. Findstr works on numeric data without having to cast, and I bet it is much faster.
Just my .02.

Zcnt = findstr(Qj,[0 0]);
Zcnt = length(Zcnt(diff([1 Zcnt])~=1));

Subject: count regions of consecutive zeros in a series

From: Jos

Date: 12 Nov, 2009 12:25:03

Message: 8 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <hdf1r5$mif$1@fred.mathworks.com>...
> I would say that one should avoid regexp on numeric data unless it is unavoidable. Findstr works on numeric data without having to cast, and I bet it is much faster.
> Just my .02.
>
> Zcnt = findstr(Qj,[0 0]);
> Zcnt = length(Zcnt(diff([1 Zcnt])~=1));

Of course you're right ;-)

Subject: count regions of consecutive zeros in a series

From: Daphne

Date: 19 Jan, 2011 08:01:06

Message: 9 of 12


Going back to this older post - how would you find the locations of the start of the zeros?

Daphne


"Jos (10584)" wrote in message <hdguqv$pu5$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hdf1r5$mif$1@fred.mathworks.com>...
> > I would say that one should avoid regexp on numeric data unless it is unavoidable. Findstr works on numeric data without having to cast, and I bet it is much faster.
> > Just my .02.
> >
> > Zcnt = findstr(Qj,[0 0]);
> > Zcnt = length(Zcnt(diff([1 Zcnt])~=1));
>
> Of course you're right ;-)

Subject: count regions of consecutive zeros in a series

From: Bruno Luong

Date: 19 Jan, 2011 08:19:04

Message: 10 of 12

SpliVec eases the task:
http://www.mathworks.com/matlabcentral/fileexchange/24255-consecutive-vector-spliter

Q = [1.2 5.4 6.2 3 0 0 0 0 2.1 2.3 4.5 6.9 7.2 3.1 0 0 1.1 2.4 5.5]

[v idx0] = SplitVec(Q>0,[],'firstval','first')
idx0(v==0)
>> idx0(v==0)

ans =

     5
    15

% Bruno

Subject: count regions of consecutive zeros in a series

From: Matt Fig

Date: 19 Jan, 2011 18:01:05

Message: 11 of 12

We could also modify a previous solution by just dropping the call to LENGTH:

Zcnt = findstr(Qj,[0 0])
STRT = Zcnt(diff([1 Zcnt])~=1)

Subject: count regions of consecutive zeros in a series

From: Daphne

Date: 23 Jan, 2011 10:07:03

Message: 12 of 12


Thanks!
That solves my problem



"Matt Fig" wrote in message <ih78t1$jq9$1@fred.mathworks.com>...
> We could also modify a previous solution by just dropping the call to LENGTH:
>
> Zcnt = findstr(Qj,[0 0])
> STRT = Zcnt(diff([1 Zcnt])~=1)

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