# [Numpy-discussion] Re: calculating on matrix indices

Bill Baxter wbaxter at gmail.com
Thu Feb 16 19:36:02 CST 2006

```Howdy,

On 2/17/06, Brian Blais <bblais at bryant.edu> 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')

This was the line causing the type error.  t is type double (float64).  'f'
makes x be type float32.  That causes the assignment below to fail.
Replacing that line with

x=zeros(len(t),'d')

should work.  Or the zeros_like() that Travis suggested.

>>
> >> 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)
> >>

You could also use
idx=where(t>5)
In place of
idx=(t>5)

Although in this case it probably doesn't make much difference, where(expr)
is more directly equivalent to matlab's find(expr).

See http://www.scipy.org/Wiki/NumPy_for_Matlab_Users for more Matlab
equivalents.  And consider contributing your own, if you have some good ones