2014-10-02 81 views
1

我想合併兩個相同尺寸的蒙版數組。我在一個網格上工作,並且對用網格定義的數組的兩部分進行計算。 當我已經獲得了2個蒙面陣列,我想在同一個維數組中組合它(總計2個結果)...但是蒙版「殲滅」了另一個部分數組的結果。Python - 組合2個相同尺寸的蒙版陣列

換句話說,當我總結2個結果時,我想停止掩碼的效果。

import numpy as np 
import numpy.ma as ma 
import matplotlib.pyplot as plt 

#I define the grid and the first masked array 
x0 = 3. 
y0 = 10. 
Lx=20. 
Ly=20. 

YA, XA = np.mgrid[0:Ly, 0:Lx] 


Theta1 = np.arctan((YA-y0)/(XA-x0)) 

mask = np.fromfunction(lambda i, j: (i >= 0) * (j >= (x0)), (XA.shape[0], XA.shape[1]), dtype='int') 
test = np.invert(mask) 
test 


V1_test = np.ma.array(Theta1, mask=test) 


plt.imshow(V1_test,aspect='auto',cmap=plt.cm.hot,origin="lower") 
plt.colorbar() 
plt.show() 


#The second masked array 
Theta2 = np.arctan((YA-y0)/(XA-x0)) + 2*np.pi 

mask2 = np.fromfunction(lambda i, j: (i >= 0) * (j < (x0)), (XA.shape[0], XA.shape[1]), dtype='int') 
test2 = np.invert(mask2) 
test2 


V1_test_2 = np.ma.array(Theta2, mask=test2) 


plt.imshow(V1_test_2,aspect='auto',cmap=plt.cm.hot,origin="lower") 
plt.colorbar() 
plt.show() 

#Combine 2 masks arrays 
test = V1_test + V1_test_2 


plt.imshow(test,aspect='auto',cmap=plt.cm.hot,origin="lower") 
plt.colorbar() 
plt.show() 
+0

你想一旦你總結的結果(和之後),以徹底擺脫面具的? – Evert 2014-10-02 08:58:26

+0

是的,我想改變我的掩碼值爲0,以總結2陣列,它可能是最簡單的方法不是嗎? – user3601754 2014-10-02 09:03:49

+1

使用['numpy.ma.filled(array,0)'](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.filled.html#numpy.ma.filled)。 – Evert 2014-10-02 09:12:38

回答

1

使用numpy.ma.filled

>>> import numpy as np 
>>> data = np.arange(10, 20) 
>>> data = np.ma.array(data, mask=np.zeros(data.shape)) 
>>> data.mask[[3,5,6]] = True 
>>> data 
masked_array(data = [10 11 12 -- 14 -- -- 17 18 19], 
      mask = [False False False True False True True False False False], 
     fill_value = 999999) 

>>> np.ma.filled(data, 0) 
array([10, 11, 12, 0, 14, 0, 0, 17, 18, 19])