Skip to content Skip to sidebar Skip to footer

Is There A Numpy.where() Equivalent For Row-wise Operations?

I want to find the index of first occurence of some condition row-wise, such that it returns a vector. I would need something like an axis=0 condition in np.where or the pylab find

Solution 1:

I think what you're looking for here isn't where, which will return you an array of elements from one of two different arrays depending on the condition, but argmax, which returns you the index of the maximum value—or, for a 2D array, the indices of the maximum value of each row or column.

But you don't want the maximum value, you want the values that are 1, right? Well, d==1 is an array of booleans, and True is greater than False, so:

In [43]: np.argmax(d==1, axis=1)
Out[43]: array([1, 1, 0, 3])

Post a Comment for "Is There A Numpy.where() Equivalent For Row-wise Operations?"