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

Ryan Krauss ryanlists@gmail....
Tue Mar 20 07:32:19 CDT 2007


Sorry to contribute to this discussion out of boredom and curiosity,
but what about:

y=where(t>0,1.0,0)

(assuming t already exists as some time vector i.e. t=arange(-1,5,0.01))

Since where is built in, you could argue that step is built in as well :0

Ryan

On 3/20/07, David Warde-Farley <david.warde.farley@utoronto.ca> wrote:
> 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
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>


More information about the SciPy-user mailing list