[Numpy-discussion] easy way to change part of only unmasked elements value?

Chao YUE chaoyuejoy@gmail....
Mon Sep 10 11:43:17 CDT 2012


Dear all numpy users,

what's the easy way if I just want to change part of the unmasked array
elements into another new value? like an example below:
in my real case, I would like to change a subgrid of a masked numpy array
to another value, but this grid include both masked and unmasked data.
If I do a simple array[index1:index2, index3:index4] = another_value, those
data with original True mask will change into False. I am using numpy 1.6.2.
Thanks for any ideas.

In [91]: a = np.ma.masked_less(np.arange(10),5)

In [92]: or_mask = a.mask.copy()
In [93]: a
Out[93]:
masked_array(data = [-- -- -- -- -- 5 6 7 8 9],
             mask = [ True  True  True  True  True False False False False
False],
       fill_value = 999999)


In [94]: a[3:6]=1

In [95]: a
Out[95]:
masked_array(data = [-- -- -- 1 1 1 6 7 8 9],
             mask = [ True  True  True False False False False False False
False],
       fill_value = 999999)


In [96]: a = np.ma.masked_array(a,mask=or_mask)

In [97]: a
Out[97]:
masked_array(data = [-- -- -- -- -- 1 6 7 8 9],
             mask = [ True  True  True  True  True False False False False
False],
       fill_value = 999999)

Chao

-- 
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120910/3a54deeb/attachment.html 


More information about the NumPy-Discussion mailing list