Skip to content Skip to sidebar Skip to footer

Numpy: Summing Every Element Of Numpy Array With Every Element Of Another

I'm coming to python from Matlab. In Matlab, given two vectors that are not necessarily the same length, they can be added if one is a row vector and one is a column vector. v1 = [

Solution 1:

import numpy as np

v1 = np.array([1, 3, 5, 7])
v2 = np.array([2, 4, 6])

v1 + v2[:, None]

You can read more about numpy's broadcasting rules.


Solution 2:

Try this out:

for i in v2:
  z = []
  for j in v1:
    z.append(i+j)
  
  print(z)

Post a Comment for "Numpy: Summing Every Element Of Numpy Array With Every Element Of Another"