Julia的R's Match版本? [英] Julia version of R's Match?
问题描述
来自R的match()
帮助页面:
说明:
'match'返回其(第一个)匹配项位置的向量 第二个是第一个参数.
‘match’ returns a vector of the positions of (first) matches of its first argument in its second.
也就是说,我可以给出两个向量,match(v1,v2)
返回一个向量,其中第i个元素是v1[i]
出现在v2
中的索引.
That is, I can give two vectors, match(v1,v2)
returns a vector where the i-th element is the index where v1[i]
appears in v2
.
朱莉娅有类似的功能吗?我找不到它.
Is there such a similar function for Julia? I cannot find it.
推荐答案
It sounds like you're looking for indexin
(just as search fodder, this is also called ismember
by Matlab). It is very slightly different: it returns a vector where the i'th element is the last index where 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
对于v1
中未出现在v2
中的元素的索引返回零.因此,只需通过非零索引编制索引,即可重构" v2
中v1
的部分:
It returns zero for the index of an element in v1
that does not appear in v2
. So you can "reconstruct" the parts of v1
that are in v2
simply by indexing by the nonzero indices:
julia> v2[idxs[idxs .> 0]]
3-element Array{Int64,1}:
8
6
7
如果您查看实现,则会看到它使用字典来存储和查找索引.这意味着它只对v1
和v2
进行一次遍历,而不是通过v2
在v1
中搜索每个元素.在几乎所有情况下,它都应该更有效率.
If you look at the implementation, you'll see that it uses a dictionary to store and look up the indices. This means that it only makes one pass over v1
and v2
each, as opposed to searching through v2
for every element in v1
. It should be much more efficient in almost all cases.
如果重要的是匹配R的行为并返回第一个索引,我们可以取消基本实现并向后构建字典,以便较低的索引覆盖较高的索引:
If it's important to match R's behavior and return the first index, we can crib off the base implementation and just build the dictionary backwards so 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
这篇关于Julia的R's Match版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!