Julia 版本的 R 的比赛? [英] Julia version of R's Match?

查看:12
本文介绍了Julia 版本的 R 的比赛?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 R 的 match() 帮助页面:

From R's help pages of 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.

Julia 有没有类似的功能?我找不到它.

Is there such a similar function for Julia? I cannot find it.

推荐答案

听起来你正在寻找 indexin(就像搜索素材,Matlab也叫ismember).它略有不同:它返回一个向量,其中第 i 个元素是 last 索引,其中 v1[i] 出现在 v2 中.

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

如果您查看实现,您会看到它使用字典来存储和查找索引.这意味着它只对 v1v2 进行一次遍历,而不是在 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 的比赛?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆