[英]How to filter 3D array with a 2D mask
I have a (m,n,3)
array data
and I want to filter its values with a (m,n)
mask to receive a (x,3)
output
array.我有一个
(m,n,3)
数组data
,我想用(m,n)
掩码过滤它的值以接收(x,3)
output
数组。
The code below works, but how can I replace the for loop with a more efficient alternative?下面的代码有效,但我怎样才能用更有效的替代方法替换 for 循环?
import numpy as np
data = np.array([
[[11, 12, 13], [14, 15, 16], [17, 18, 19]],
[[21, 22, 13], [24, 25, 26], [27, 28, 29]],
[[31, 32, 33], [34, 35, 36], [37, 38, 39]],
])
mask = np.array([
[False, False, True],
[False, True, False],
[True, True, False],
])
output = []
for i in range(len(mask)):
for j in range(len(mask[i])):
if mask[i][j] == True:
output.append(data[i][j])
output = np.array(output)
The expected output is预期的输出是
np.array([[17, 18, 19], [24, 25, 26], [31, 32, 33], [34, 35, 36]])
import numpy as np
data = np.array([
[[11, 12, 13], [14, 15, 16], [17, 18, 19]],
[[21, 22, 13], [24, 25, 26], [27, 28, 29]],
[[31, 32, 33], [34, 35, 36], [37, 38, 39]],
])
mask = np.array([
[False, False, True],
[False, True, False],
[True, True, False],
])
output = data[mask]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.