Python Forum
How to vectorize a calculation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to vectorize a calculation
#1
I have a 1D array containing ending values of a financial computation that has n elements and a matrix of inflation factors that has n rows and w columns. I'd like to divide each member of the 1D array with the product of column values of the corresponding row in the matrix. I know I can do this in a loop:

for i in range(n):
   b[i] /= numpy.product(infl[i,:]))
But I'd like to vectorize it to save (a lot) of time because n is big (1-2M). Is there a way to do this?
Reply
#2
What about
b /= infl.prod(axis=1)
Reply
#3
(Feb-03-2022, 08:01 AM)Gribouillis Wrote: What about
b /= infl.prod(axis=1)

Works like a charm, many thanks!! Smile
Reply


Forum Jump:

User Panel Messages

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