Yes, indeed your(@ptrblck) solution works pretty well. For anyone who’s looking for solution using torch see the snippet below:
import torch

# numerator: tensor([2., 2., 0., 5., 7., 3., 4., 3., 6., 5.])
a = torch.randint(0, 10, (10,), dtype=torch.float32)

# denominator: tensor([3., 3., 0., 4., 5., 4., 7., 8., 0., 4.])
b = torch.randint(0, 10, (10,), dtype=torch.float32)

# initialize output tensor with desired value
c = torch.full_like(a, fill_value=float('nan'))

# zero mask
mask = (b != 0)

# finally perform division
c[mask] = a[mask] / b[mask]

# output: tensor([0.6667, 0.6667,    nan, 1.2500, 1.4000, 0.7500, 0.5714, 0.3750,    nan, 1.2500])