Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operation in Numpy multi-dimensional array

Tags:

numpy

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?

like image 733
Rocky Avatar asked Aug 28 '26 23:08

Rocky


1 Answers

Write it like you say it:

b[a==0] = np.nan
like image 178
kabanus Avatar answered Aug 30 '26 16:08

kabanus