I'm looking for a faster way to find the first indices of repeated elements in two dependent arrays. For one array a solution is trivial with np.unique(a, axis=0, return_index=True).
I want to convert my iterative algorithm below to run faster with numpy.
import numpy as np
a = np.asarray([(1, 2), (2, 2), (1, 3), (1, 2), (5, 5), (5, 5), (5, 5), (1, 2), (1, 2)])
b = np.asarray([(3, 4), (5, 2), (1, 3), (6, 2), (5, 2), (5, 5), (0, 0), (0, 0), (8, 8)])
# ^ ^ ^ repeat repeat ^ repeat repeat repeat
# first first first in a in b first in a in a in a
# idx 0 idx 1 idx 2 (6, 2) (5, 5) idx 5 (0, 0) (0, 0) (8, 8)
# ignored ignored ignored ignored ignored
# My algorithm
indexes = []
aset = set()
bset = set()
for i in range(len(a)):
if tuple(a[i]) not in aset and tuple(b[i]) not in bset:
aset.add(tuple(a[i]))
bset.add(tuple(b[i]))
indexes.append(i)
print(indexes)
# output: [0,1,2,5] (correct)
print(aset)
# output: {(1, 2), (1, 3), (5, 5), (2, 2)}
print(bset)
# output: {(1, 3), (3, 4), (5, 5), (5, 2)}
However, it is difficult to vectorize because it is dependent on the previously seen data. I implemented a vectorized algorithm that doesn't give the desired result.
# Incorrect numpy conversion
_, indieces0 = np.unique(a, axis=0, return_index=True)
_, indieces1 = np.unique(b, axis=0, return_index=True)
indieces = np.intersect1d(indieces0, indieces1)
print(indieces)
# output: [0,1,2] (Wrong)
Is it possible to vectorize the algorithm?