I have 2 numpy arrays:
a= np.array([[2, 1, 7],
[7, 7, 3],
[1, 7, 4]])
b= np.array([9,-1,17])
I would like to change the 7 in each column in a with the values from b, such that in the first column the 7 is replaced by the first value from b and in the second column the 7s are replaced by the second value from b and so on.
My simple solution is:
for j in range(len(b)):
a[a[:,j]==7,j]=b[j]
array([[ 2, 1, 17],
[ 9, -1, 3],
[ 1, -1, 4]])
It works, but for very large matrices it is not fast enough. Is there another, faster way to do this?