4

What I want to do is "mask" a subset of an array of j elements, from range 0 to k. Eg. For this array:

[0.2, 0.1, 0.3, 0.4, 0.5]

Masking the first 2 elements it becomes

[NaN, NaN, 0.3, 0.4, 0.5] 

Does masked_array support this operation?

2 Answers 2

6
In [51]: arr=np.ma.array([0.2, 0.1, 0.3, 0.4, 0.5],mask=[True,True,False,False,False])

In [52]: print(arr)
[-- -- 0.3 0.4 0.5]

Or, if you already have a numpy array, you could use np.ma.masked_less_equal (see the link for a variety of other operations for masking particular elements):

In [53]: arr=np.array([0.2, 0.1, 0.3, 0.4, 0.5])

In [56]: np.ma.masked_less_equal(arr,0.2)
Out[57]: 
masked_array(data = [-- -- 0.3 0.4 0.5],
             mask = [ True  True False False False],
       fill_value = 1e+20)

Or, if you wish to mask the first two elements:

In [67]: arr=np.array([0.2, 0.1, 0.3, 0.4, 0.5])

In [68]: arr=np.ma.array(arr,mask=False)

In [69]: arr.mask[:2]=True

In [70]: arr
Out[70]: 
masked_array(data = [-- -- 0.3 0.4 0.5],
             mask = [ True  True False False False],
       fill_value = 1e+20)
Sign up to request clarification or add additional context in comments.

Comments

1

I found this:

ma.array([1,2,3,4], mask=[1,1,0,0]) masked_array(data = [-- -- 3 4], mask = [ True True False False], fill_value = 999999)

Comments

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.