calculating on matrix indices

B

Brian Blais

Hello,

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)
tau=5
x[idx]=exp(-t[idx]/tau) # <---this line is wrong (gives a TypeError)

#------------------

what is the best way to replace the wrong line with something that works: replace all
of the values of x at the indices idx with exp(-t/tau) for values of t at indices idx?

I do this all the time in matlab scripts, but I don't know that the pythonic
preferred method is.



thanks,

bb
 
C

Colin J. Williams

Brian said:
Hello,

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 is the best way to replace the wrong line with something that
works: replace all of the values of x at the indices idx with
exp(-t/tau) for values of t at indices idx?

I do this all the time in matlab scripts, but I don't know that the
pythonic preferred method is.



thanks,

bb
Brian,

What are you trying to do? It is most unlikely that you need Boolean
values in x[idx]

Colin W.
 
K

Kirk McDonald

Brian said:
Hello,

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)
tau=5
x[idx]=exp(-t[idx]/tau) # <---this line is wrong (gives a TypeError)

#------------------

what is the best way to replace the wrong line with something that
works: replace all of the values of x at the indices idx with
exp(-t/tau) for values of t at indices idx?

I do this all the time in matlab scripts, but I don't know that the
pythonic preferred method is.



thanks,

bb

You're specifying the type of x but not of t. You need to change the
line where you assign t to:

t = arange(0, 20, .1, 'f')

I'm not sure why it doesn't figure that own on its own (since it
obviously does hold floats), but this does cause it to work.

-Kirk McDonald
 
B

Brian Blais

Colin said:
Brian said:
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
 
R

Robert Kern

Brian said:
Colin said:
Brian said:
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]

[Apologies for piggybacking.]

Boolean arrays are perfectly valid indices and do exactly what the OP wants.

In [25]: t[idx]
Out[25]:
array([ 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9,
6. , 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8,
6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7,
7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6,
8.7, 8.8, 8.9, 9. , 9.1, 9.2, 9.3, 9.4, 9.5,
....

In [28]: (t[idx] == compress(idx, t)).all()
Out[28]: True

When used as indices, Boolean arrays are treated as arrays of Boolean values,
not 0s and 1s. Arrays of 0s and 1s used as indices would get the first and
second values of an array, respectively, as I think you were expecting.

In [35]: t[idx.astype(int)]
Out[35]:
array([ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
...

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top