I have an array of unspecified dimension (it may be 1D, 2D, 3D, 4D, ...). I want to apply to reorder on the last dimension using an array of the index of the same size as the last dimension. I know how to do it with a dummy if ... if statement:
import numpy as np
a = np.ones((10, 10, 20, 40,30,10))
index=np.arange(a.shape[-1])
if len(a.shape)==1:
a=a[index]
elif len(a.shape)==2:
a=a[:,index]
elif len(a.shape)==3:
a=a[:,:,index]
elif len(a.shape)==4:
a=a[:,:,:,index]
elif len(a.shape)==5:
a=a[:,:,:,:,index]
else:
print('Dimensions too high!!!')
I am of course not satisfied with this because it cannot be generalized to any dimension. I would be very grateful if someone could indicate to me the correct way to do it!
Thanks!