使用Python中的条件语句查找2D数组的索引 [英] Find indices of 2D arrays using conditional statement in Python
问题描述
我有两个二维数数组,即(95,60)维的纬度和经度.我正在尝试查找以下条件语句的索引.
I have two 2D number arrays i.e., lat and lon each of (95,60) dimensions. I am trying to find the indices for the following conditional statement.
ind = np.where(((lat >= 20.0 and lat <= 30.0) and (lon >= 90.0 and lon <= 100.0)))
我收到以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我的经纬度数组包含如下数据:
My lat array contains the data like this:
array([[ 19.13272095, 19.52386856, 19.8377533 , ..., 22.81465149,
22.80446053, 22.78530312],
[ 19.2490139 , 19.64085197, 19.95526123, ..., 22.93462563,
22.92414474, 22.90457916],
[ 19.36528587, 19.75781822, 20.07275009, ..., 23.05456543,
23.04379272, 23.02381706],
...,
[ 29.7395134 , 30.20263481, 30.57126617, ..., 33.85147858,
33.81478882, 33.75755692],
[ 29.85359764, 30.31761169, 30.68692398, ..., 33.97143555,
33.93445587, 33.87679672],
[ 29.9676609 , 30.4325695 , 30.80256653, ..., 34.09137726,
34.05410767, 33.99602509]], dtype=float32)
类似地,lon数组包含如下数据:
Similarly, lon array contains the data as follows:
array([[ 96.78914642, 98.04283905, 99.0661087 , ..., 119.39452362,
120.45418549, 121.74817657],
[ 96.75196075, 98.00632477, 99.03016663, ..., 119.37508392,
120.43569183, 121.73084259],
[ 96.71466064, 97.96970367, 98.99414062, ..., 119.35567474,
120.41724396, 121.71356201],
...,
[ 92.90063477, 94.2386322 , 95.33486176, ..., 117.72390747,
118.90248108, 120.34107971],
[ 92.85238647, 94.19155884, 95.2888031 , ..., 117.7070694 ,
118.88733673, 120.32800293],
[ 92.80393219, 94.14429474, 95.24256897, ..., 117.69021606,
118.87218475, 120.31491089]], dtype=float32)
我尝试如下修改命令:
ind = np.where(((lat.all() >= 20.0 and lat.all() <= 30.0) and (lon.all() >= 90.0 and lon.all() <= 100.0)))
我得到以下空数组作为输出:
I get the following empty array as output:
(array([], dtype=int64),)
我什至尝试了以下方法:
I have even tried the following:
ind = np.where(lat.all() >= 20.0 and lat.all() <= 30.0)
获得的输出为:(array([], dtype=int64),)
我不知道如何解决我的问题.谁能帮助我我在哪里做错了什么?
I don't understand how to solve my problem. Can anyone help me how and where I am doing wrong?
推荐答案
这需要进行两个修复:()首先执行比较,然后用元素级&
This needs two fixes, () to perform the comparisons first, and replacing the scalar and
with an elementwise &
ind = np.where((((lat >= 20.0) & (lat <= 30.0)) & ((lon >= 90.0) & (lon <= 100.0))))
当在需要标量布尔值的上下文中使用布尔数组时,会发生此ValueError. if lat >= 20.0:
是常见情况. and
是Python短路运算符,因此实际上会执行if
测试.
This ValueError arises when an boolean array is used in a context that needs a scalar boolean. if lat >= 20.0:
is common case. and
is a Python short circuiting operator, so in effect does a if
test.
我们还需要在&
仅包含lat
数组的一部分:
Out[48]: (array([], dtype=int32), array([], dtype=int32))
In [49]: np.where((((lat >= 20.0) & (lat <= 30.0))))
Out[49]:
(array([0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 4, 5], dtype=int32),
array([3, 4, 5, 3, 4, 5, 2, 3, 4, 5, 0, 0, 0], dtype=int32))
这篇关于使用Python中的条件语句查找2D数组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!