help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: filtering logical array


From: Jordi Gutiérrez Hermoso
Subject: Re: filtering logical array
Date: Tue, 26 Jun 2012 14:48:18 -0400

On 26 June 2012 14:22, seektime <address@hidden> wrote:
> Hi, suppose you have a logical array such as
>
>> a=[0 0 1 1 1 1 1 0 1 0 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1 1 0];
>
> I'd like to filter this vector so it looks like this:
>
>> a
>  a=[0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0];
>
> So consecutive 1's of 3 or more remain in the output while all other 1's are
> set to false.

Here's a pretty stupid solution...

    d = diff(a);
    s = find(d > 0) + 1;
    e = find(d < 0) + 1;
    n = e - s;

Now n contains the size of each streak of ones and s contains the
indices of where they start.

This assumes a begins and ends with 0, but I'm sure you can figure out
how to adapt this.

HTH,
- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]