Select rows in a Numpy 2D array with a boolean vector
numpy, python
Solution
The shape of `b` is somewhat strange, but if you can craft it as a nicer index it's a simple selection:
idx = b.reshape(a.shape[0])
print a[idx==0,:]
>>> [[10 11 12 13 14]]
You can read this as, "select all the rows where the index is 0, and for each row selected take all the columns". Your expected answer should really be a list-of-lists since you are asking for all of the rows that match a criteria.
Problem
I have a matrix and a boolean vector: ``` >>>from numpy import * >>>a = arange(20).reshape(4,5) array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) >>>b = asarray( [1, 1, 0, 1] ).reshape(-1,1) array([[1], [1], [0], [1]]) ``` Now I want to select all the corresponding rows in this matrix where the corresponding index in the vector is equal to zero. ``` >>>a[b==0] array([10]) ``` How can I make it so this returns this particular row? ``` [10, 11, 12, 13, 14] ```