How to get the index of a element in a Tensor whose value is True?

Hi guys,
I meet a problem that, how to get the index of a element in a Tensor whose value is True?
Such as, a Tensor like:
False False Fasle
Flase True False
False False False
Then I can get the index of (1,1).
So how can I implement this operation?

Your answer and idea will be appreciated!

1 Like

nonzero() would return you the indices of all non-zero entries (in that case True):

x = torch.bernoulli(torch.ones(3, 3) * 0.5).bool()
print(x)
> tensor([[ True,  True, False],
        [False, False,  True],
        [ True, False, False]])
print(x.nonzero())
> tensor([[0, 0],
        [0, 1],
        [1, 2],
        [2, 0]])
2 Likes

Thanks sincerely for your answer and guide!