[SciPy-User] re[SciPy-user] moving for loops...

Zachary Pincus zachary.pincus at yale.edu
Fri May 21 09:11:34 EDT 2010


> import numpy as np
>
> numpts=10
> tsteps = 12
> vari = 22
>
> data = np.random.random((tsteps, vari, numpts, 1))
> new_data = np.zeros((tsteps, numpts), dtype=np.float32)
> index = np.arange(numpts)
>
> for i in xrange(tsteps):
>    for j in xrange(numpts):
>        new_data[i,j] = data[i,5,index[j],0]
>

new_data2 = data[:,5,index,0].astype(numpy.float32)
numpy.all(new_data == new_data2) # returns True

This assuming that your real "index" array is more interesting than  
just [0,1,2,3,...]... if not,
new_data2 = data[:,5,:,0].astype(numpy.float32)
would do fine.

That said, I've never been able to figure out whether it's possible to  
index particular points along multiple axes with index lists -- that  
is, how to make numpy do something like:
new_data3 = data[index_x,5,index_y,0]

Zach



More information about the SciPy-User mailing list