I have two arrays and the function
nparr1 = np.array([1,2])
nparr2 = np.array([[0.4, 0.8, 1.2, 1.6, 2]])
def fun(a,b):
return a + b
I would like my output to be matrix
result = [
[fun(1,0.4), fun(1,0.8), fun(1,1.2), fun(1,1.6), fun(1,2)],
[fun(2,0.4), fun(2,0.8), fun(2,1.2), fun(2,1.6), fun(2,2)]
]
I figured out that running
np.array(np.meshgrid(nparr1, nparr2)).T
results in intermediate step of building this matrix
intermidiate_matrix = [[[1. 0.4]
[1. 0.8]
[1. 1.2]
[1. 1.6]
[1. 2. ]]
[[2. 0.4]
[2. 0.8]
[2. 1.2]
[2. 1.6]
[2. 2. ]]]
It looks like a step in the right direction. But I am not sure how to proceed from here. Summation in the fun
is arbitrary and used only as an example. The key things is that function takes two params and reduces it to single value. I was looking into np.vectorize
but didn't have much sucess.