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 = [1 3 5 7]
v2 = [2 4 6]'
v1 + v2
ans =
3 5 7 9
5 7 9 11
7 9 11 13
I am trying to produce the same behavior in python given two numpy arrays. Looping first came to mind:
import numpy as np
v1 = np.array([1,3,5,7])
v2 = np.array([2,4,6])
v3 = np.empty((3,4,))
v3[:] = np.nan
for i in range(0,3):
v3[i,:] = v1 + v2[i]
Is there a more concise and efficient way?
size(v1)is (1,4),size(v2)is (3,1). Just make thenumpyarrays the same shape. (though thenumpyv1is ok with (4,) shape).