I have an array A[i,j]
. The last index contains various input values for a function myfunc
that will be applied for each i
and produce an output B[i]
. However, many values indexed by j
will not contribute to B
, so I would like to avoid unnecessary calls of myfunc
. This can be accomplished by slicing out the relevant values with conditional indexing such as C = C[C>mythreshold]
with for loops relatively easily as in the MWE below:
def myfunc(X):
return np.square(X).sum()
A = np.floor(np.random.rand(3,4)*100)
mythreshold = 10
(N1, N2) = A.shape
B = np.zeros(N1)
for i in range(N1):
C = A[i,:]
C = C[C>mythreshold]
B[i] = myfunc(C)
I had to break this up into for loops so I could remove slices of A
without removing slices of the full array. This was since I cannot drop elements of A[i,:]
for one i
without dropping the corresponding elements for another i
. For speed, however, I would like to vectorize wherever possible - to avoid for loops and do this for all i
in one go. How can I do this?
Note: That was an MWE; the actual case has larger array dimensions, so that my arrays would be A[i,j,k,l]
and B[i,j]
, so the for loop example would be something like the code below. I think the extra dimensions wouldn't complicate things, but it's worth mentioning just in case.
(N1, N2, N3, N4) = A.shape
for i in range(N1):
for j in range(N2):
C = A[i,j,:,:].flatten()
C = C[C>mythreshold]
B[i,j] = myfunc(C)