For simplicity, take the small example below. Lets say we have 2 sets of numpy arrays, values and distances. I'd like to find values that are above 1 and sort them by its corresponding distances. If there are values with similar distances, I'd like to have it sorted with the higher value first.
v = np.array([[1.0,2.0,0.0],[1.0,0.0,0.0],[0.0,0.0,0.0]])
d = np.array([[1.5,1.0,1.5],[1.0,0.0,1.0],[1.5,1.0,1.5]])
indexes = np.argwhere(v >= 1)
list = ( ((d[r,c],v[r,c],(r,c))) for r, c in indexes)
closest_highest = sorted(list,key=lambda t: (t[0],-t[1]))
print(closest_highest)
output:
[(1.0, 2.0, (0, 1)), (1.0, 1.0, (1, 0)), (1.5, 1.0, (0, 0))]
Each tuple contains the distance, value, and its coordinates from the two arrays.
Is there a faster way to do the above using just numpy/vectorized computations? If not, is there a faster/more efficient way to do the following? I dont really need it to return a tuple, just the index is enough. Even just the index of the lowest distance with the highest value is enough.
If there are values with similar distances, I'd like to have it sorted with the higher value first- Which higher values? Apart from the distances, there are only row and column indices.