Solved by Emre_Yalcinoglu in post #2 
              
              
                Hi! 
I don’t know any compact way to achieve this, however, using the function torch.mul might be useful for this task. Here’s a possible solution: 
A = torch.randn(5,5)
B = torch.randint(1,4,(5,5)).float()
Z = torch.zeros(5,5).float()

# I_n is a matrix where I_n(i,j) = 1 iff B(i,j)=n and for else …