Arrays Python-组合2个具有相同维度的掩码数组
我想合并两个相同维度的屏蔽数组。我在一个网格上工作,我在用网格定义的数组的两个部分上进行计算。 当我获得2个蒙版数组时,我想将它(2个结果之和)合并到一个相同维度的数组中…但是蒙版“湮灭”了另一部分数组的结果 换句话说,当我对2个结果求和时,我想停止遮罩的效果Arrays Python-组合2个具有相同维度的掩码数组,arrays,numpy,combinations,mask,Arrays,Numpy,Combinations,Mask,我想合并两个相同维度的屏蔽数组。我在一个网格上工作,我在用网格定义的数组的两个部分上进行计算。 当我获得2个蒙版数组时,我想将它(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
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()
将numpy导入为np
将numpy.ma导入为ma
将matplotlib.pyplot作为plt导入
#我定义了网格和第一个屏蔽数组
x0=3。
y0=10。
Lx=20。
Ly=20。
YA,XA=np.mgrid[0:Ly,0:Lx]
θ1=np.arctan((YA-y0)/(XA-x0))
mask=np.fromfonment(lambda i,j:(i>=0)*(j>=(x0)),(XA.shape[0],XA.shape[1]),dtype='int')
测试=np.反转(遮罩)
试验
V1_测试=np.ma.阵列(θ1,掩码=测试)
plt.imshow(V1_测试,aspect='auto',cmap=plt.cm.hot,origin=“lower”)
plt.colorbar()
plt.show()
#第二掩蔽阵列
θ2=np.arctan((YA-y0)/(XA-x0))+2*np.pi
mask2=np.fromfunction(λi,j:(i>=0)*(j<(x0)),(XA.shape[0],XA.shape[1]),dtype='int')
test2=np.invert(mask2)
测试2
V1_test_2=np.ma.阵列(θ2,掩码=test2)
plt.imshow(V1\u test\u 2,aspect='auto',cmap=plt.cm.hot,origin=“lower”)
plt.colorbar()
plt.show()
#组合2个遮罩阵列
测试=V1_测试+V1_测试2
plt.imshow(测试,aspect='auto',cmap=plt.cm.hot,origin=“lower”)
plt.colorbar()
plt.show()
使用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])
你想在对结果求和后(以及之后)完全去掉掩码吗?是的,我想在0中转换我的掩码值,以便对2个数组求和,这可能是最简单的方法,不是吗?使用。谢谢!;)正是我想要的want@SaulloCastro为了子孙后代,我可能应该这样做。它看起来应该是复制品,但我在别处找不到。