Using Torch.Meshgrid with missing data

I am working with a directed acyclic graph that models the nx random variables over nt time steps. I built a mesh grid representing positional coordinates in this graph to retrieve the value of the variable xt,i where t is the time coordinate and i is one of the nx random variables.

How can I obtain a list of key-value pair coordinates where the key is a pair of time (t_idx) and positional (x_idx) coordinate and the value is the next imediate coordinate where the variable is observed? It is possible to compute a boolean mask to know where the data is observed.

# time and positional coordinate
t_idx , x_idx = torch.meshgrid(torch.arange(arg.nt, out=torch.LongTensor()),
                               torch.arange(arg.nx, out=torch.LongTensor())
                              )

# time and positional coordinate with observed data 
mask_data = (~torch.isnan(train_data)).squeeze(-1).nonzero()

I’ve found a way to do build a list that permits retrieving this key-pair value but this may not be the best solution…

coordinate = (torch.isnan(train_data) != True).squeeze(-1).nonzero()
# tensor([[0, 0], [0, 1],[0, 2],[4, 1], [5, 2],[6, 0]])
idx_pair = []
for id_va in set(coordinate[:, 1]):
    all_tidx = torch.where((coordinate == id_va))[0]
    for x, y in zip(all_tidx, all_tidx[1:]):
        idx_pair.append(torch.stack((coordinate[x][0], coordinate[y][0], coordinate[y][1])))
idx_pair = torch.stack(idx_pair)
# output = tensor([[0, 3, 0],
#        [1, 4, 1],
#        [2, 5, 2],
#        [5, 6, 2]])