[SciPy-user] I can't find unit step function.

David Warde-Farley david.warde.farley@utoronto...
Tue Mar 20 03:29:34 CDT 2007


On Tue, 2007-03-20 at 03:45 -0400, Anne Archibald wrote:

> In [7]: def step(x):
>    ...:     return asarray(x>=0,dtype=float)
>    ...:
> 
> In [8]: step(linspace(-1,1,9))
> Out[8]: array([ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.,  1.])
> 
> Though if you prefer step(0)==0.5 it's a bit of a pain.

For the sake of completeness and utter boredom, here's the vectorized
version with step(0) == 0.5, and it's still a one-liner ;)

def step(x):
    return asarray(x>0,dtype=float)+0.5*asarray(x==0,dtype=float)

In [9]: step(arange(-5,5))
Out[9]: array([ 0. ,  0. ,  0. ,  0. ,  0. ,  0.5,  1. ,  1. ,  1. ,
1. ])

Cheers,

David



More information about the SciPy-user mailing list