Reputation: 69
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)))
I get the following error:
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)
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)
I tried to revise my command as follows:
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)
Output is obtained as: (array([], dtype=int64),)
I don't understand how to solve my problem. Can anyone help me how and where I am doing wrong?
Upvotes: 1
Views: 207
Reputation: 14399
Never liked a big pile of parentheses and ampersands. I usually use functools.reduce
in this situation.
from functools import reduce
ind = np.where(reduce(np.logical_and , (lat >= 20.0 , lat <= 30.0 , lon >= 90.0 , lon <= 100.0)))
Upvotes: 0
Reputation: 231728
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))))
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.
We also need to do the (<=)
tests before the &
With just part of the lat
array:
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))
Upvotes: 2