#### Python + 2-D array slicing + valueerror: operands could not be broadcast together

`````` 1. a 1-D array of longitudes
2. a 1-D array of latitudues
3. a 2-D array of some quantity (sized len(lat),len(long))
``````

``````ii = find( (lon >= xlims[0]) & (lon <= xlims[1]) )
jj = find( (lat >= ylims[0]) & (lat <= ylims[1]) )
z=array[jj, ii]

ValueError: shape mismatch: objects cannot be broadcast to a single shape

I have tried this using a boolean approach

ii = ( (lon >= xlims[0]) & (lon <= xlims[1]) )
jj = ( (lat >= ylims[0]) & (lat <= ylims[1]) )
``````

1 个回复

``````>>> lon = np.arange(10)
>>> lat = np.linspace(40,50,17)
>>> quant = np.random.random((len(lon),len(lat)))
>>> ii = (lon >= 2) & (lon < 5)
>>> jj = (lat >= 42) & (lat <= 44)
``````

``````>>> ii
array([False, False,  True,  True,  True, False, False, False, False, False], dtype=bool)
>>> jj
array([False, False, False, False,  True,  True,  True, False, False,
False, False, False, False, False, False, False, False], dtype=bool)
``````

``````>>> np.ix_(ii,jj)
(array([[2],
[3],
[4]]), array([[4, 5, 6]]))
``````

``````>>> quant[np.ix_(ii,jj)]
array([[ 0.51567424,  0.84138194,  0.6267137 ],
[ 0.1865154 ,  0.7785198 ,  0.16720573],
[ 0.80563691,  0.82990892,  0.28402503]])
``````
1 Python：ValueError：操作数不能与形状一起广播（101）（2）

2017-06-20 22:38:44 1 703   numpy
2 Python - ValueError：操作数无法与形状一起广播

3 解决“ ValueError：操作数不能一起广播”

4 Python：numpy数组操作数不能一起广播

2019-11-27 06:02:42 0 24   scipy
5 尝试使用numpy实现2d图像卷积会引发ValueError：操作数无法一起广播

6 ValueError：操作数无法与形状一起广播 (3,) (3000,)

2020-07-09 10:32:11 1 42   physics
8 ValueError：操作数不能与形状一起广播-Keras

9 Scikit学习-ValueError：操作数不能一起广播

10 熊猫过滤ValueError：操作数不能与形状一起广播

2018-02-14 20:04:58 1 176   pandas