It looks like you are looking for indexin (just like search feed, this is also called ismember by Matlab). It is slightly different: it returns a vector in which the ith element is the last index in which v1[i] appears in v2 .
julia> v1 = [8,6,7,11]; v2 = -10:10; idxs = indexin(v1, v2) 4-element Array{Int64,1}: 19 17 18 0
It returns zero for the index of the item in v1 , which does not appear in v2 . This way you can βrestoreβ parts of v1 that are in v2 simply by indexing with non-zero indexes:
julia> v2[idxs[idxs .> 0]] 3-element Array{Int64,1}: 8 6 7
If you look at the implementation , you will see that it uses a dictionary to store and search indexes. This means that it only passes v1 and v2 each, as opposed to searching through v2 for each element in v1 . It should be much more effective in almost all cases.
If it is important to combine the behavior of R and return the first index, we can discard the basic implementation and simply build the dictionary back so that the lower indices overwrite the higher ones:
function firstindexin(a::AbstractArray, b::AbstractArray) bdict = Dict{eltype(b), Int}() for i=length(b):-1:1 bdict[b[i]] = i end [get(bdict, i, 0) for i in a] end julia> firstindexin([1,2,3,4], [1,1,2,2,3,3]) 4-element Array{Int64,1}: 1 3 5 0 julia> indexin([1,2,3,4], [1,1,2,2,3,3]) 4-element Array{Int64,1}: 2 4 6 0