Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access n-th dimension in python [duplicate]

I want a easy to read access to some parts of a multidimensional numpy array. For any array accessing the first dimension is easy (b[index]). Accessing the sixth dimension on the other hand is "hard" (especially to read).

b[:,:,:,:,:,index] #the next person to read the code will have to count the :

Is there a better way to do this? Especially is there a way, where the axis is not known while writing the program?

Edit: The indexed dimension is not necessarily the last dimension

like image 722
Jürg Merlin Spaak Avatar asked Jul 13 '26 17:07

Jürg Merlin Spaak


1 Answers

You can use np.take. For example:

b.take(index, axis=5)
like image 51
MB-F Avatar answered Jul 15 '26 05:07

MB-F



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!