我有一个充满布尔值的数组:
array([[[ True, True, False, False, False, False],
[ True, False, False, False, False, False]],
[[False, False, True, False, True, False],
[ True, False, False, False, False, False]],
[[ True, False, False, False, False, False],
[ True, False, False, False, False, False]]], dtype=bool)
我想得到每一行每一列中第一次出现True的索引,所以答案会是这样的:
array([[0,0,0],
[0,1,0],
[1,0,2],
[1,1,0],
[2,0,0],
[2,1,0]])
有没有一个简单而快速的方法来做到这一点?
3条答案
按热度按时间czq61nw11#
现在不能测试,但我认为这应该工作
argmax
在numpy 1.9中的bools短路,因此对于此用例,它应该优于where
或nonzero
。EDIT好的,所以上面的解决方案不起作用,但是
argmax
的方法仍然有用:3ks5zfa02#
似乎您希望
np.where()
与the solution of this answer结合使用来查找唯一的行:arknldoa3#
添加到其他答案上,如果你想让它给予你第一个为True的列的索引,并返回
n
,其中n
= # cols ina
,如果一行不包含True: