change array elements dependent on index - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: change array elements dependent on index (/thread-22665.html) |
change array elements dependent on index - SchroedingersLion - Nov-21-2019 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 RE: change array elements dependent on index - scidam - Nov-22-2019 It depends on some_function . In any case, you can use numpy.vectorize decorator. It turns your function in another which acceptvector 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 ).
|