Python Forum

Full Version: change array elements dependent on index
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings,

is there an easier way to write the following with some kind of slicing arithmetics?

for k in range(1, N-1):
    A[k] = some_function(k) * some_array[k]
where A and some_array are numpy arrays and some_function returns a double.


Best,

SL
It depends on some_function. In any case, you can use numpy.vectorize decorator. It turns your function in another which accept
vector arguments (however, it is just a for-loop).
import numpy as np

@np.vectorize
def some_function(k):
    pass

A = some_function(np.arange(1, N-1)) * some_array[1:-1]
Another option would be considering using of numba.jit (JIT-compiler) and implement vectorized
version of your function (some_function).