I have a data array containing ndim coordinates of N particles over timesteps 1 to M. The columns in the array typically represent the (x,y,z) of each particle 'p', and each row in the array represents another time point 't':
x_t1p1 y_t1p1 z_t1p1 x_t1p2 y_t1p2 z_t1p2 ... x_t1pN y_t1pN z_t1pN
x_t2p1 y_t2p1 z_t2p1 x_t2p2 y_t2p2 z_t2p2 ... x_t2pN y_t2pN z_t2pN
...
x_tMp1 y_tMp1 z_tMp1 x_tMp2 y_tMp2 z_t1p2 ... x_tMpN y_tMpN z_tMpN
I would like to convert the array to a 3D format such that each particle is in a different (M x ndim) 'slice' of the numpy array. I am currently doing the following:
import numpy as np
def datarray_to_3D(data, ndim=3):
(nr,nc) = data.shape
nparticles = nc/ndim
dat_3D = np.zeros([nr,ndim,nparticles])
for i in range(nparticles):
dat_3D[:,:,i] = data[:,i*ndim:(i+1)*ndim]
return dat_3D
I have a basic knowledge of NumPy, but would like to increase my proficiency in array manipulation. How can the above function be rewritten to eliminate the loop and use a more 'NumPythonic' structure?
Thank you.
-c