How to create an array consists of indices of elements in one array that exists in the other array?

First, if this is an array of 2-component coordinate vectors, I would strongly recommend using an array of StaticArrays instead, e.g.

using StaticArrays
coord2 = [SVector(1.0,0.0), SVector(2.0,0.0), SVector(1.0,1.0), SVector(2.0,1.0)]

which will be vastly more efficient for virtually any computation you might want to perform.

What algorithm you might want to use here depends on how long your coord1 and coord2 arrays are and whether you care about efficiency. The double loop in your implementation has complexity \Theta(N_1 N_2) if your arrays have lengths N_1 and N_2, respectively. If you want something faster for large arrays (or if you just want shorter code) you could, for example, use a dictionary mapping coordinates to indices:

dict1 = Dict(c => i for (i, c) in enumerate(coord1))
dict2 = Dict(c => i for (i, c) in enumerate(coord2))
index1 = [dict2[c] for c in coord1 if haskey(dict2, c)]
index2 = [dict1[c] for c in coord2 if haskey(dict1, c)]
2 Likes