[SciPy-user] Extract local minima

Alan G Isaac aisaac at american.edu
Mon Jun 19 16:18:56 CDT 2006


On Mon, 19 Jun 2006, Nils Wagner apparently wrote:
> Is there a way to extract local minima from the column 
> vector stored in data.mtx ? 

See below.

Cheers,
Alan Isaac



from numpy import arange, rand,ones, dtype, empty
from pylab import plot, show, scatter 
def find_minima(x):
	minima =  empty((len(x),),dtype=bool)
	dx =  x[1:]-x[:-1]
	minima[1:-1] = (dx[:-1]<=0) & (dx[1:]>=0)
        #handle endpoints
	minima[0]=dx[0]>=0
	minima[-1]=dx[-1]<=0
	return minima

NOBS=100
x = rand(NOBS) 
minima = find_minima(x)
domain = arange(NOBS)
plot(domain,x,'r-') 
scatter(domain[minima],x[minima],s=4) 



More information about the SciPy-user mailing list