1

I would like to replace the values of a 2D numpy array based on a mask from another array. The idea is that values in arr_b should be set to 0.8 in the locations where arr_a value equals 0.4. Both arr_a and arr_b are always going to have the same size. And for the purpose of this toy example, you can assume that arr_a has sevreral values that are 0.4. But the code is not working:

import numpy
arr_a = numpy.random.rand(20,40)
arr_b = numpy.random.rand(20,40)
arr_a[0,1] = 0.4

mask_cntr = numpy.ma.masked_not_equal(arr_a[:], 0.4)
ma_arr = numpy.ma.masked_where(mask_cntr, arr_b)
ma_arr.filled(fill_value = 0.8)

Can someone tell me how to fix this?

1
  • What is it doing, and how is that wrong? Why are you using np.ma methods? Commented Sep 16, 2015 at 3:38

1 Answer 1

1

Your array definitions are wrong, as you can read in your error message.

arr_a = numpy.random.random((20,40))
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, this was a typo in my copy pasting here, but the code does corretly create the random array

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.