Numpy binary matrix - get rows and columns of True elements

前端 未结 1 1090
长情又很酷
长情又很酷 2021-01-06 08:49

I have a binary numpy 2D array, say,

import numpy as np
arr = np.array([
#   Col 0   Col 1  Col 2
    [False, False, True],  # Row 0
    [True, False, False]         


        
1条回答
  •  一整个雨季
    2021-01-06 09:07

    You are looking for np.argwhere -

    np.argwhere(arr)
    

    Sample run -

    In [220]: arr
    Out[220]: 
    array([[False, False,  True],
           [ True, False, False],
           [ True,  True, False]], dtype=bool)
    
    In [221]: np.argwhere(arr)
    Out[221]: 
    array([[0, 2],
           [1, 0],
           [2, 0],
           [2, 1]])
    

    0 讨论(0)
提交回复
热议问题