How can I create a mask based on comparing numerical values?

Hey guys,

How can I create a mask based on comparing numerical values? For example, I want the mask to be 1 when the first value in the second dimension is bigger than the second value in the second dimension.

Input: [[1, 2], [4, 1], [3, 1], [0, 1]]
Output Mask: [0, 1, 1, 0]

Input: [[22, 11], [90,80], [15, 19], [6, 8]]
Output Mask: [1, 1, 0, 0]

Is this possible? Thank you for your help in advance!

EDIT: Corrected second example

This should work:

x = torch.tensor([[1, 2], [4, 1], [3, 1], [0, 1]])
res = (x[:, 0] > x[:, 1]).long()

x = torch.tensor([[22, 11], [90,80], [15, 19], [6, 8]])
res = (x[:, 0] > x[:, 1]).long()

The second output should probably be [1, 1, 0, 0]?

1 Like

It does work :smile: And you are right about the second example, I will correct it. Thank you for your help!