Julia Version R Match?

From the R help pages in match() :

Description:

'Match returns the position vector of the (first) matches of its first argument in its second.

That is, I can give two vectors, match(v1,v2) returns a vector where the ith element is the index, where v1[i] appears in v2 .

Is there such a similar feature for Julia? I can not find it.

+5
source share
2 answers

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 
+6
source

I don’t think it exists out of the box, but as @Hasha (and Tim Sacred Answer to another question) points out, you should quickly find your own definition. First try:

 function matched(v1::Array, v2::Array) matched = zeros(length(v1)) for i = 1:length(v1) matched[i] = findfirst(v2, v1[i]) end return matched end 

(note that I called the matched function because match defined in Base to match strings, if you want to expand it, you need to import Base.match first). You could do it faster by applying some of the tricks from the Julia docs performance section if you care about performance.
This function should do what you are looking for, if I understand correctly, try it, for example,

 v1 = [rand(1:10) for i = 1:100] v2 = [rand(1:10) for i = 1:100] matched2(v1,v2) 
+4
source

All Articles