【发布时间】:2014-10-02 08:33:21
【问题描述】:
我想合并两个相同维度的掩码数组。我在网格上工作,并对用网格定义的数组的两个部分进行计算。 当我获得了 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
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 以便对 2 个数组求和,这可能是最简单的方法,不是吗?
-
谢谢! ;) 正是我想要的
-
@SaulloCastro 我可能应该,为了后代。它似乎应该是重复的,但我在其他地方找不到它。
标签: arrays numpy combinations mask