Search code examples
vectorluatorch

torch.Tensor manipulation - Comparing two vectors


I've got two tensor objects repenting vectors: (0110010), (0111011) I would like to compare between the two and create a new tensor vector: (0110010) Iterating over them in a loop is very slow, I know there is a solution for this in Matlab so I assume there is one for tensors as well.


Solution

  • To do a logical and operation for tensors containing only 1 and 0 elements you could use the :cmul() member function (element-wise multiplication).

    th> torch.Tensor({0,1,1,0,0,1,0}):cmul(torch.Tensor({0,1,1,1,0,1,1}))
     0
     1
     1
     0
     0
     1
     0
    

    To compare two tensors element-wise you can use :eq():

    th> torch.Tensor({0,1,1,0,0,1,0}):eq(torch.Tensor({0,1,1,1,0,1,1}))
     1
     1
     1
     0
     1
     1
     0