renan-brso
renan-brso

Reputation: 149

How to update a numpy array based on a masked array w/ same dimension and shape

I have two arrays with same dimension and shape. this nv_arr array has null values (-9999). I've created a masked array nv_mask to mask all values besides -9999 to somehow "update" this out array by the position, without the need to create a new array.

In other words, numbers 0, 3, 6 and 2 must be replaced by -9999. How can I do that?

    >>> nv_arr
     ([[-9999,   123,   -9999],
       [-9999,   444,   444],
       [-9999,   323,   323]])

>>> nv_mask
[[-9999 -- -9999]
 [-9999 -- --]
 [-9999 -- --]]

 >>> out
      ([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

Upvotes: 1

Views: 454

Answers (1)

cs95
cs95

Reputation: 402553

Assuming nv_mask is a MaskedArray, you can call the .mask attribute to set values:

>>> out[~nv_mask.mask] = -9999
>>> out 
array([[-9999,     1, -9999],
       [-9999,     4,     5],
       [-9999,     7,     8]])

Upvotes: 1

Related Questions