import torch
h, w = 8, 8
c = 2
a = torch.randn(h*w, c)

S_mat = torch.cdist(a, a)

i_ind = torch.tensor([12, 14, 51]).long()
j_ind = torch.tensor([21, 60, 33]).long()

result = S_mat[i_ind, j_ind]

In fact, the tensor a is very large, suppose h, w = 100, 100,  so I do not want to calculate distance of all the pairs of points, instead only serveral selected indexes. Is there any way to perform it ?