Why non-consecutive indexing does not work in 1D

I am just curious. Why multi-index selection (e.g (1,2,4)) works on multidimensional tensor, but does not work on 1D tensors. Does it use different data structure? Quick example:

# 1D tensor
t1 = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8])
# 2D tensor
t2 = torch.tensor([[1,2,3,4,5,6,7,8]])

# FOR 2D
# select 1st, 3rd and 5th element from 2D
t2[:, (0,2,4)]
# output: tensor([[1, 3, 5]])

# FOR 1D, try the same
# output: IndexError: too many indices for tensor of dimension 1

I can still select the same values by using either torch.index_select() or by doing this:
t1[torch.tensor([0,2,4])] or t1[[0,2,4]] which are the same, except wrapped in a tensor or list.

I would just like to understand the reasoning here, why the pattern that works in 2D+ does not work in 1D? Where’s the logic? :slight_smile:

I’m not a specialist of advanced indexing, but I think that passing a single tuple has the meaning of “these are the coordinates of the point I want”. So in your case that would be for a 3D Tensor.

If you want to index a single dimension, you can do: t1[(0,2,4),]