Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python creating convolve function for arrays

I have two arrays,

a = [3, 6, 8, 2, 5, 5]
b = [2, 7, 9]

and I need to create a new array c which takes the values and adds them like this: a[0+0]*b[0] + a[0+1]*b[1] + a[0+2]*b[2] = (3*2) + (6*7) + (9*8) = 6 + 42 + 72 which means c[0] = 120

I'm completely lost on how to do this anything to point me in the right direction would be awesome.

like image 761
iamtesla Avatar asked Jan 19 '26 06:01

iamtesla


1 Answers

If c[k] = a[k+0]*b[0] + a[k+1]*b[1] + a[k+2]*b[2]

then

>>> c = [sum(i*j for i,j in zip(a[k:], b)) for k in range(4)]
>>> c
[120, 86, 75, 84]
like image 88
John La Rooy Avatar answered Jan 20 '26 19:01

John La Rooy