I have two multi-dimensional numpy array. I would like to convert the entry in the second array to NaN, if the corresponding element in first is zero. Below is example to manually mimic the same: (Can this be done programmatically)
import numpy as np
a = np.random.rand(4,5)
a[0][0] = 0
a[1][0] = 0
a[1][1] = 0
b = np.random.rand(4,5)
b[0][0] = np.nan
b[1][0] = np.nan
b[1][1] = np.nan
Can we use masking here?
for i in range(len(a)): for j in range(len(a[i])): if (a[i][j] == 0) b[i][j] = np.nan