[SciPy-user] Finding Gaps in an Array
Lorenzo Isella
lorenzo.isella@gmail....
Mon Jul 6 12:22:07 CDT 2009
> Date: Mon, 06 Jul 2009 10:18:07 +0200
> From: Lorenzo Isella <lorenzo.isella@gmail.com>
> Subject: [SciPy-user] Finding Gaps in an Array
> To: scipy-user@scipy.org
> Message-ID: <4A51B33F.7060306@gmail.com>
> 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)
>
> 1240070020
> 1240070040
> 1240070060
> 1240070080
> 1240070100
> 1240070180
> 1240070200
> 1240070620
> 1240070640
> 1240070880
> 1240070900
> 1240070940
> 1240070980
> 1240071000
> 1240071020
> 1240071040
> 1240071060
> 1240071080
> 1240071100
> 1240071120
> 1240071140
> 1240071160
> 1240071180
> 1240071200
> 1240071220
> 1240071240
> 1240071260
> 1240071280
> 1240071300
>
> 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
> (1240070200-1240070180).
> How can you automatically calculate these intervals?
> Any suggestion is welcome.
> Cheers
>
> Lorenzo
>
>
>
>
> Date: Mon, 6 Jul 2009 05:01:14 -0700 (PDT)
> From: David Baddeley <david_baddeley@yahoo.com.au>
> Subject: Re: [SciPy-user] Finding Gaps in an Array
> To: SciPy Users List <scipy-user@scipy.org>
> Message-ID: <672254.30337.qm@web33004.mail.mud.yahoo.com>
> Content-Type: text/plain; charset=utf-8
>
>
> Hi Lorenzo,
>
> numpy.diff(your_array)
>
> 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
>
> cheers,
> David
>
>
>
>
Thanks David,
But I also need something more.
Let us make it real easy. Consider the array
import numpy as n
f=n.array([0,1,2,4,9,22,23,24,32,33,59,60,76])
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
[3,3,2,2].
Does anyone know how to do this efficiently?
Cheers
Lorenzo
More information about the SciPy-user
mailing list