Numpy Adding two vectors with different sizes Numpy Adding two vectors with different sizes python python

Numpy Adding two vectors with different sizes


This could be what you are looking for

if len(a) < len(b):    c = b.copy()    c[:len(a)] += aelse:    c = a.copy()    c[:len(b)] += b

basically you copy the longer one and then add in-place the shorter one


If you know that b is higher dimension, then:

>>> a.resize(b.shape)>>> c = a+b

is all you need.


Very similar to the one above, but a little more compact:

l = sorted((a, b), key=len)c = l[1].copy()c[:len(l[0])] += l[0]