1

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]])

1 Answer 1

1

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]])
Sign up to request clarification or add additional context in comments.

2 Comments

Almost correct ;-P .mask is True at the masked values, not the valid ones.
@PaulPanzer Ah, I see I botched up the initialisation of these arrays, so my answer needed a ~ it didn't have. Ty!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.