[Numpy-discussion] Re: calculating on matrix indices
Ryan Krauss
ryanlists at gmail.com
Thu Feb 16 19:16:06 CST 2006
Brian's example works fine for me as long as
x=zeros(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=zeros(len(t),'f')
In [319]: t=arange(0,20,.1)
In [320]: idx=where(t>5)
In [321]: x[idx]=exp(-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=zeros(len(t),'d')
In [323]: x[idx]=exp(-t[idx]/tau)
In [324]:
Ryan
On 2/16/06, Travis Oliphant <oliphant.travis at ieee.org> 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=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
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
> 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=lnk&kid=103432&bid=230486&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
More information about the Numpy-discussion
mailing list