[Numpy-discussion] Re: calculating on matrix indices
Travis Oliphant
oliphant.travis at ieee.org
Thu Feb 16 19:02:02 EST 2006
Brian Blais wrote:
> Colin J. Williams wrote:
>
>> Brian Blais wrote:
>>
>>> In my attempt to learn python, migrating from matlab, I have the
>>> following problem. Here is what I want to do, (with the wrong syntax):
>>>
>>> from numpy import *
>>>
>>> t=arange(0,20,.1)
>>> x=zeros(len(t),'f')
>>>
>>> idx=(t>5) # <---this produces a Boolean array,
>>> probably not what you want.
>>> tau=5
>>> x[idx]=exp(-t[idx]/tau) # <---this line is wrong (gives a TypeError)
>>>
>> What are you trying to do? It is most unlikely that you need Boolean
>> values in x[idx]
>>
>
> in this example, as in many that I would do in matlab, I want to
> replace part of a vector with values from another vector. In this
> case, I want x to be zero from t=0 to 5, and then have a value of
> exp(-t/tau) for t>5. I could do it with an explicit for-loop, but
> that would be both inefficient and unpython-like. For those who know
> matlab, what I am doing here is:
>
from numpy import *
t = r_[0:20:0.1]
idx = t>5
tau = 5
x = zeros_like(t)
x[idx] = exp(-t[idx]/tau)
Should do it.
-Travis
More information about the NumPy-Discussion
mailing list