<div class="question-status question-originals-of-duplicate">
This question already has an answer here:
Logical indexing in Numpy with two indices as in MATLAB
I am trying to write some code that uses logical numpy arrays to index a larger array, similar to how MATLAB allows array indexing with logical arrays.
import numpy as np m = 4 n = 4 unCov = np.random.randint(10, size = (m,n) ) rowCov = np.zeros( m, dtype = bool ) colCov = np.ones( n, dtype = bool ) >>> unCov[rowCov, rowCov]  # as expected >>> unCov[colCov, colCov] [0 8 3 3] # diagonal values of unCov, as expected >>> unCov[rowCov, colCov] ValueError: shape mismatch: objects cannot be broadcast to a single shape
For this last evaluation, I expected an empty array, similar to what MATLAB returns. I'd rather not have to check rowCov/colCov for True elements prior to indexing. Why is this happening, and is there a better way to do this?
As I understand it, numpy will translate your 2d logical indices to actual index vectors:
arr[[True,False],[False,True]] would become
arr[0,1] for an
ndarray of shape
(2,2). However, in your last case the second index array is full
False, hence it corresponds to an index array of length 0. This is paired with the other full
True index vector, corresponding to an index array of length 4.
If the index arrays do not have the same shape, there is an attempt to broadcast them to the same shape. If they cannot be broadcast to the same shape, an exception is raised:
In your case, the error is exactly due to this:
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-1411-28e41e233472> in <module>() ----> 1 unCov[colCov,rowCov] IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (4,) (0,)
MATLAB, on the other hand, automatically returns an empty array if the index array is empty along any given dimension.<hr>
This actually highlights a fundamental difference between the logical indexing in MATLAB and numpy. In MATLAB, vectors in subscript indexing always slice out a subarray. That is, both
will return the
2 x 2 submatrix of the matrix
arr. If the logical index vectors are shorter than the given dimension of the array, the missing indexing elements are assumed to be
false. Fun fact: the index vector can also be longer than the given dimension, as long as the excess elements are all
false. So the above is also equivalent to
4 x 4 array (for the sake of argument).
In numpy, however, indexing with boolean-valued numpy
arrays in this way will try to extract a vector. Furthermore, the boolean index vectors should be the same length as the dimension they are indexing into. In your
4 x 4 example,
both return the two first diagonal elements, so not a submatrix but rather a vector. Furthermore, they also give the less-then-encouraging warning along the lines of
/usr/bin/ipython:1: VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 4 but corresponding boolean dimension is 5
So, in numpy, your logical indexing vectors should be the same length as the corresponding dimensions of the
ndarray. And then what I wrote above holds true: the logical values are translated into indices, and the result is expected to be a vector. The length of this vector is the number of
True elements in every index vector, so if your boolean index vectors have a different number of
True elements, then the referencing doesn't make sense, and you get the error that you get.