gpt4 book ai didi

arrays - Julia 的负/补索引,如 R

转载 作者:行者123 更新时间:2023-12-02 09:53:45 25 4
gpt4 key购买 nike

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

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

[1] 2 3 4 6 8 9 10

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

最佳答案

这与 @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

关于arrays - Julia 的负/补索引,如 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52378061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com