# Get indices of repeated columns

Hi everyone,

I stumbled over the following problem that I couldn’t solve myself:

Given a 4 x n tensor `A` which might contain non-unique columns, I’m looking for a way to get the column index of a single (could be the first, but does not have to) occurrence of each column.

Fore example given the tensor `A` (note that column 1 and 5 are identical)

``````tensor([[10., 11., 11., 11., 11., 11., 13., 13., 14., 14.],
[ 0.,  2.,  0.,  1.,  1.,  2.,  1.,  1.,  0.,  0.],
[ 3.,  6.,  6.,  2.,  6.,  6.,  9.,  3.,  4.,  7.],
[ 2.,  3., 10., 10., 10.,  3.,  5.,  6.,  6.,  9.]])
``````

I would like to obtain the following indices

``````tensor([[ 0.,  1.,  2.,  3.,  4.,  6.,  7.,  8.,  9.]])  # Index of 1st occurrence
# or
tensor([[ 0.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.]])  # Index of 2nd occurrence
``````

If getting the indices of the columns is not possible, a mask would also do:

``````tensor([[ 1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1., 1.]])  # Masking 2nd occurrence
# or
tensor([[ 1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1., 1.]])  # Masking 1st occurrence
``````

I hope it is somewhat clear what I’m trying to achieve. Maybe I’m over-complicating things and there is an easier way around this (see background information below). In any case help is really appreciated =)

Cheers!

Background information
The columns of the tensor `A` above represents the locations of targets in a 4D grid. The targets themselves are stored in a tensor of the same length of `A`. When constructing my loss, I need to make sure, that there is only one target in each grid cell. My idea is to search the tensor `A` for non-unique columns and then only keep the column index of a single occurrence. Using this list of indices I could then select the valid targets.