arrays - Python-组合2个具有相同尺寸的掩码数组

标签 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
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.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])

关于arrays - Python-组合2个具有相同尺寸的掩码数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26157092/

相关文章:

java - 在数组中正确使用协方差

php - 错误号 : 1048 Column 'btc' cannot be null

ios - 使用按钮时数组索引超出范围

python - 一个数据帧与另一个数据帧的相关矩阵

java - 在 Java 中查找给定数组的所有可能组合

c# - 生成字符串列表的所有组合

algorithm - 拼车预订的数据库/算法

javascript - 创建一个数组的数组

python - 将值插入任意二维坐标 numpy 矩阵

python - 在一张图中绘制两个散点图