vendredi 11 septembre 2015

Iterating efficiently through indices of arbitrary order array

Say I have an arbitrary array of variable order N. For example: A is a 2x3x3 array is an order 3 array with 2,3, and 3 dimiensions along it's three indices.

I would like to efficiently loop through each element. If I knew a priori the order then I could do something like (in python),

#for order 3 
import numpy as np
shape = np.shape(A)
i = 0
while i < shape[0]:
  j = 0
  while j < shape[1]:
    k = 0
    while k < shape[2]:
      #code using i,j,k
      k += 1
    j += 1
  i += 1

Now suppose I don't know the order of A, i.e. I don't know a priori the length of shape. How can I permute the quickest through all elements of the array?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire