Search code examples
luatorch

Torch, how to select a subset of a tensor?


For example, I have a tensor t = torch.rand(10,1). And I want to select the tensor subset of at the rows of {1,3,7,9} and concatenate them as new tensor. Is there any elegant way? Thank you.


Solution

  • There's :index method:

    tens = torch.rand(10,1)
    ind = torch.LongTensor{1,3,7,9}
    subset = tens:index(1, ind)