|
From: Ryan K. <rya...@gm...> - 2006-02-17 03:15:55
|
Brian's example works fine for me as long as x=3Dzeros(len(t),'d') for some reason the error seems to come from assignment of a double array to a single precision array: In [318]: x=3Dzeros(len(t),'f') In [319]: t=3Darange(0,20,.1) In [320]: idx=3Dwhere(t>5) In [321]: x[idx]=3Dexp(-t[idx]/tau) --------------------------------------------------------------------------- exceptions.TypeError Traceback (most recent call last) /home/ryan/thesis/accuracy/<ipython console> TypeError: array cannot be safely cast to required type In [322]: x=3Dzeros(len(t),'d') In [323]: x[idx]=3Dexp(-t[idx]/tau) In [324]: Ryan On 2/16/06, Travis Oliphant <oli...@ie...> wrote: > 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=3Darange(0,20,.1) > >>> x=3Dzeros(len(t),'f') > >>> > >>> idx=3D(t>5) # <---this produces a Boolean array, > >>> probably not what you want. > >>> tau=3D5 > >>> x[idx]=3Dexp(-t[idx]/tau) # <---this line is wrong (gives a TypeErro= r) > >>> > >> 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=3D0 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 =3D r_[0:20:0.1] > idx =3D t>5 > tau =3D 5 > x =3D zeros_like(t) > x[idx] =3D exp(-t[idx]/tau) > > > Should do it. > > -Travis > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. Do you grep through log fi= les > for problems? Stop! Download the new AJAX search engine that makes > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! > http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=3D103432&bid=3D230486&dat= =3D121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |