calculating on matrix indices
Brian Blais
bblais at bryant.edu
Thu Feb 16 21:44:26 EST 2006
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:
t=0:.1:20;
idx=find(t>5);
tau=5;
x=zeros(size(t));
x(idx)=exp(-t(idx)/tau)
is that clearer? I am sure there is a nice method to do this in python, but I
haven't found it in the python or numpy docs.
thanks,
bb
--
-----------------
bblais at bryant.edu
http://web.bryant.edu/~bblais
More information about the Python-list
mailing list