domotorp
domotorp

Reputation: 113

How to address elements of multidimensional array with variables?

import numpy as np
cube = np.zeros((2,2,2,2,2,2,2)) # Make 7 dim hypercube
vector=[1,0,1,1,0,1,1]
cube[vector[0],vector[1],vector[2],vector[3],vector[4],vector[5],vector[6]] # access the field [1,0,1,1,0,1,1]

I have to work with some high dimensional arrays and I would like to access their fields through variables. The above code shows a very bad solution and I am sure that there is a more efficient one, something similar to cube[vector] or cube[vector[i] for i in range(len(vector))] but none of them seem to work.

Upvotes: 1

Views: 79

Answers (1)

xnx
xnx

Reputation: 25508

NumPy indexes are just tuples, so you can:

cube[tuple(vector)]

Upvotes: 3

Related Questions