Julia 的负/补索引,如 R

Nov*_*tat 5 arrays indexing julia

Julia 中是否有类似于 R 负索引的功能?在 R 中,代码类似于:

x = 1:10
inds = c(1, 5, 7)
x[-inds]

[1]  2  3  4  6  8  9 10
Run Code Online (Sandbox Code Playgroud)

我发现这在许多情况下都非常有用,特别是对于采样索引以创建测试/训练集之类的事情,而且还可以对数组进行子索引以排除某些行。所以我希望 Julia 中有一些简单的东西可以做到同样的事情。

Jul*_*ner 3

这与 @Colin T Bower 的答案类似,并且也仅使用基本 Julia。恐怕它不像你的 R 示例那么优雅。

julia> minus(indx, x) = setdiff(1:length(x), indx)
minus (generic function with 1 method)

julia> x = collect(1:10)
10-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

julia> inds = [1, 5, 7]
3-element Array{Int64,1}:
 1
 5
 7

julia> x[minus(inds, x)]
7-element Array{Int64,1}:
  2
  3
  4
  6
  8
  9
 10
Run Code Online (Sandbox Code Playgroud)