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
  All Array Index JohnnyCoffee 3 633 Feb-26-2021, 06:46 AM
Last Post: ndc85430
  Sorting Elements via parameters pointing to those elements. rpalmer 3 780 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  4D array with only elements on one side of the diagonal schniefen 0 531 Dec-24-2020, 11:32 AM
Last Post: schniefen
  LIST or ARRAY Comparison and change of value nio74maz 0 483 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  How i can add elements to table index of element blazej2533 3 623 Dec-03-2020, 08:16 PM
Last Post: Larz60+
  Removing some elements from array based on a condition claw91 0 551 Oct-27-2020, 03:42 PM
Last Post: claw91
  Create new variable dependent on two existing variables JoeOpdenaker 6 948 Oct-25-2020, 02:15 PM
Last Post: jefsummers
  display single elements in array davidm 3 789 Jul-26-2020, 07:32 AM
Last Post: davidm
  Python scipy odeint: solving with solution-dependent functions etienne 0 844 Jun-05-2020, 01:29 PM
Last Post: etienne
  change array column values without loop khalidreemy 2 1,383 May-05-2019, 09:05 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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