查找具有两个约束的2D数组的索引

发布于 2025-02-11 01:29:24 字数 305 浏览 2 评论 0原文

我有两个相等大小的2D数组,并且正在尝试识别满足两个条件的索引。这是我尝试的和我得到的。有什么建议吗?我正在使用np.Where,这似乎不是正确的选择。

感谢您的帮助。

ind_direct_pos = np.where((bz_2D_surface3 > 0.0) and (jz_2D_surface3 > 0.0))

valueerror:一个以上元素的数组的真实价值是 模糊的。使用a.any()或a.all()

I have two 2D arrays in NumPy of equal size, and am trying to identify the indices where two conditions are met. Here's what I try and what I get. Any suggestions? I'm using np.where and this does not seem to be the correct choice.

Thanks for any help.

ind_direct_pos = np.where((bz_2D_surface3 > 0.0) and (jz_2D_surface3 > 0.0))

ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静赏你的温柔 2025-02-18 01:29:24

如果两个具有相同大小的数组,而不是相同的形状,则首先可以ravel(使用and而不是&是此问题的原因,如 iiahdi comment ):

bz_2D_surface3 = np.array([[1, 3], [4, -1], [-3, -6], [-1, 2], [1, 3], [2, -3]])
jz_2D_surface3 = np.array([[-1, 3, 3], [-4, -1, 2], [3, -1, -5], [1, 2, 3]])

# pair_wise
np.transpose(np.where((bz_2D_surface3.ravel() > 0) & (jz_2D_surface3.ravel() > 0)))
# [[ 1]
#  [ 2]
#  [ 9]
#  [10]]

如果我们有两个带有的阵列相同的形状

bz_2D_surface3 = np.array([[1, 3], [4, -1], [-3, -6], [-1, 2], [1, 3]])
jz_2D_surface3 = np.array([[-1, 3], [-4, -1], [3, -1], [1, 2], [1, 11]])

# pair_wise
np.transpose(np.where((bz_2D_surface3 > 0) & (jz_2D_surface3 > 0)))
# [[0 1]
#  [3 1]
#  [4 0]
#  [4 1]]

# row_wise
np.where(((bz_2D_surface3 > 0.0).all(1)) & ((jz_2D_surface3 > 0.0).all(1)))[0]
# [4]

If the two arrays with the same size, not the same shape, we can ravel it at first (using and instead & is a cause of this problem as I'mahdi comment):

bz_2D_surface3 = np.array([[1, 3], [4, -1], [-3, -6], [-1, 2], [1, 3], [2, -3]])
jz_2D_surface3 = np.array([[-1, 3, 3], [-4, -1, 2], [3, -1, -5], [1, 2, 3]])

# pair_wise
np.transpose(np.where((bz_2D_surface3.ravel() > 0) & (jz_2D_surface3.ravel() > 0)))
# [[ 1]
#  [ 2]
#  [ 9]
#  [10]]

if we have two arrays with the same shape:

bz_2D_surface3 = np.array([[1, 3], [4, -1], [-3, -6], [-1, 2], [1, 3]])
jz_2D_surface3 = np.array([[-1, 3], [-4, -1], [3, -1], [1, 2], [1, 11]])

# pair_wise
np.transpose(np.where((bz_2D_surface3 > 0) & (jz_2D_surface3 > 0)))
# [[0 1]
#  [3 1]
#  [4 0]
#  [4 1]]

# row_wise
np.where(((bz_2D_surface3 > 0.0).all(1)) & ((jz_2D_surface3 > 0.0).all(1)))[0]
# [4]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文