Python Forum

Full Version: How to vectorize a calculation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
What about
b /= infl.prod(axis=1)
(Feb-03-2022, 08:01 AM)Gribouillis Wrote: [ -> ]What about
b /= infl.prod(axis=1)

Works like a charm, many thanks!! Smile