Python Forum
change array elements dependent on index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change array elements dependent on index
#1
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
Reply
#2
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove some elements from an array in python? gohanhango 9 980 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  Modify an Energy Model to account for year dependent interest rate rather than only t giovanniandrean 0 398 Oct-10-2023, 07:00 AM
Last Post: giovanniandrean
  Change a numpy array to a dataframe Led_Zeppelin 3 1,064 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,896 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  array change svm 6 1,422 Jun-23-2022, 01:01 PM
Last Post: svm
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,011 May-17-2022, 11:38 AM
Last Post: Larz60+
  Replace elements of array with elements from another array based on a third array Cola_Reb 6 1,741 May-13-2022, 06:06 PM
Last Post: deanhystad
Question Change elements of array based on position of input data Cola_Reb 6 2,060 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  All Array Index JohnnyCoffee 3 2,496 Feb-26-2021, 06:46 AM
Last Post: ndc85430
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,548 Feb-10-2021, 04:53 PM
Last Post: rpalmer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020