Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectorizing for loop in Numpy

is there a way to vectorize this?

waveheight=zeros(10000)
for t in range(10000):
    for j in range(N_frequencysteps):
        waveheight[t] = waveheight[t] + (Abs_S_newwave[j] * cos (K[j] * x - (omega[j] * ((t*0.01) - TimeShift)) + TSi_omega[j] + arg_S_newwave[j]))
like image 650
GGrewal Avatar asked Jul 23 '26 15:07

GGrewal


1 Answers

waveheight = (Abs_S_newwave[:,None] * cos(K[:,None] * x - (omega[:,None] * ((arange(10000)[None,:]*0.01) - TimeShift)) + TSi_omega[:,None] + arg_S_newwave[:,None])).sum(axis=0)

This works if all arrays of length N_frequencysteps are 1-D numpy arrays.

like image 189
eumiro Avatar answered Jul 25 '26 05:07

eumiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!