[SciPy-User] numpy array assignment
marco cammarata
marcocamma at gmail.com
Thu Apr 17 10:59:59 EDT 2014
Hi,
I have used many times syntax like
import numpy as np
def result_of_long_calculation_returning_the_right_shape():
return np.random.random( (2,3) )
N=10
out = np.empty( (N,2,3) )
for i in range(N):
out[i] = result_of_long_calculation_returning_the_right_shape()
if I have to loop trough the axis 1 I would do something like
N=10
out = np.empty( (2,N,3) )
for i in range(N):
out[:,i,:] = result_of_long_calculation_returning_the_right_shape()
The problem is that - in the function I am writing - the number of
dimensions and the axis to loop trough are not known in advance.
Is there a smart what to do it ?
I would expect that a function like np.assign(array,element=0,axis=0) that
but I can't find such function.
For the moment I ended up doing something really really inelegant:
res = result_of_long_calculation_returning_the_right_shape()
s = "out[" + (":,")*axis + "i" + (",:")*(nDim-axis-1) + "]=res"
exec(s)
Thanks a lot in advance,
marco
More information about the SciPy-User
mailing list