Change sign of elements with an odd sum of indices
np.negative is silghtly faster than multiplying (as it is a ufunc) N = 5 arr = np.arange(N ** 3).reshape(N, N, N) %timeit arr.ravel()[1::2] *= -1 %timeit np.negative(arr.ravel()[1::2], out = arr.ravel()[1::2]) The slowest run took 8.74 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: … Read more