如何在5x2x2 NumPy数组中搜索特定的2x2矩阵?

问题描述 投票:0回答:1

我有一个尺寸为(5,2,2)的NumPy数组,它是一系列2×2矩阵。我该如何询问这些2×2矩阵之一是否具有特定值?

例如,让我们看下面的系列

import numpy as np
zeros = np.zeros(4).reshape(2,2)
serie = np.array([zeros+1, zeros+2, zeros+3, zeros+4, zeros+5])

然后,在serie中有一个2填充2的2×2矩阵。我如何询问serie是否包含一个2填充的2×2矩阵并检索其索引?在这种情况下,索引将为1,因为serie[1,:]是查找矩阵。

python numpy matrix search indexing
1个回答
1
投票

使用简单数组比较,np.allnp.all

np.where

输出:

np.where

希望有帮助!

import numpy as np

zeros = np.zeros(4).reshape(2,2)
serie = np.array([zeros+1, zeros+2, zeros+3, zeros+4, zeros+5])

to_find = zeros+2

index = np.where(np.all(serie == to_find, axis=(1, 2)))[0]
print(index)
© www.soinside.com 2019 - 2024. All rights reserved.