I am trying to add each element of a numpy array in turn to another numpy 1D array, but not as an element-wise operation.
More specifically, define func:
import numpy as np
array1 = np.array([1,2,3,4])
array2 = np.array([10,20,30])
def func(array1,array2):
#what goes here?
return output_array
output_array = func(array1,array2)
So that:
output_array = np.array([[11,12,13,14],[21,22,23,24],[31,32,33,34]])
I have managed to make it work using:
def func(array1,array2):
return np.array(list(map(lambda x: x + array1,array2)))
However, it seems like there should be a much better way to do this, and it would also be useful to generalise this to n-dimensions. I have had a try with np.vectorize():
def func(array1,array2):
np_function = np.vectorize(lambda x: x + array1)
return np_function(array2)
But this does not work as it tries to assign a sequence to a single array element inside the iterator (error "setting an array element with a sequence").