1

I have three huge numpy.arrays, and need to execute some conditional statements involving all three numpy.arrays fast. All numpy.arrays are of the same dimension (NxD)(N>2 and D>1) and of same datatype. Normally I would do as shown below

 for i in range(n):
     for j in range(d):
         if np.sign(nabla[i][j]) != np.sign(delta[i][j]):
             g[i][j] = g[i][j] + 0.2
         if np.sign(nabla[i][j]) == np.sign(delta[i][j]):
             g[i][j] = g[i][j] * 0.8

if I only had to operate with one numpy.array I would do

g[g < val] = newval

But I am receiving an error by applying the same principles since delta and nabla are more than two-dimensional.

1 Answer 1

1

You should consider using Boolean Indexing instead, e.g.:

mask = np.sign(nabla) == np.sign(delta)
g[mask] *= 0.8
g[~mask] += 0.2

Or alternatively:

g = np.where(np.sign(nabla) == np.sign(delta), g * 0.8, g + 0.2)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.