[Numpy-discussion] change masked array member values with conditional selection

Chao YUE chaoyuejoy@gmail....
Thu Apr 12 08:07:09 CDT 2012


Dear all numpy users,

I am using numpy 1.6.1

I find that if you want to change some member values in a masked array
according to some conditional selection.
suppose a is a masked array, you want to change all value below zero to
zero.
you must always use

a[np.nonzero(a<0)]=0

rather than a[a<0]=0.

the latter will lose all masked elements.


an example:
In [24]: a=np.arange(10.)

In [25]: a=np.ma.masked_array(a,mask=a<3)

In [28]: a[a<5]=2.

In [29]: a
Out[29]:
masked_array(data = [2.0 2.0 2.0 2.0 2.0 5.0 6.0 7.0 8.0 9.0],
             mask = [False False False False False False False False False
False],
       fill_value = 1e+20)



In [30]: b=np.arange(10.)

In [31]: b=np.ma.masked_array(b,mask=b<3)

In [34]: b[np.nonzero(b<5)]=2.

In [35]: b
Out[35]:
masked_array(data = [-- -- -- 2.0 2.0 5.0 6.0 7.0 8.0 9.0],
             mask = [ True  True  True False False False False False False
False],
       fill_value = 1e+20)

cheers,

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/20120412/9bdaabc7/attachment.html 


More information about the NumPy-Discussion mailing list