[SciPy-user] Finding Gaps in an Array
Mon Jul 6 12:22:07 CDT 2009
> Date: Mon, 06 Jul 2009 10:18:07 +0200
> From: Lorenzo Isella <firstname.lastname@example.org>
> Subject: [SciPy-user] Finding Gaps in an Array
> To: email@example.com
> Message-ID: <4A51B33F.firstname.lastname@example.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> Dear All,
> Suppose you have an integer array whose entries are like the one below
> (no entry is ever repeated and the array is sorted in increasing order)
> Let us assume that the entries represent contact times (in seconds)
> between 2 objects (one does not need to know anything else for this
> post). The value of the initial time does not matter (i.e. you could
> subtract it to all the elements in the array to have a list starting
> from zero). Now, you define the contact duration as the time spanned by
> consecutive entries evenly spaced by 20 seconds.
> E.g. in the array above, the first 5 elements define a contact of
> duration 1240070100-1240070020=80. Then there is an 80-sec gap
> (1240070180-1240070100) followed by a contact of duration 20
> How can you automatically calculate these intervals?
> Any suggestion is welcome.
> Date: Mon, 6 Jul 2009 05:01:14 -0700 (PDT)
> From: David Baddeley <email@example.com>
> Subject: Re: [SciPy-user] Finding Gaps in an Array
> To: SciPy Users List <firstname.lastname@example.org>
> Message-ID: <email@example.com>
> Content-Type: text/plain; charset=utf-8
> Hi Lorenzo,
> will give you the times between entries,
> numpy.where(numpy.diff(your_array) > 20)
> will give the indices where the gap was longer than 20
But I also need something more.
Let us make it real easy. Consider the array
import numpy as n
I want to calculate the length of the subarrays consisting of numbers
evenly spaced by 1 (like in the example, but now the gap is 1).
In the case of f, these subarrays are:
[0,1,2], [22,23,24], [32,33] and [59,60].
Hence, I would like to get back an array giving me the counts
Does anyone know how to do this efficiently?
More information about the SciPy-user