在Julia 1.0.2中查找函数

A. *_* A. 6 julia

我正在转换到Julia 1.0.2,我意识到找不到find函数.在之前的版本(Julia 0.6)中我可以写

find(x -> x<0, my_var)
Run Code Online (Sandbox Code Playgroud)

为了获得名为my_var的数组的负面元素.当我在Julia 1.0.2中运行相同的代码时,我收到以下错误:

UndefVarError: find not defined
Run Code Online (Sandbox Code Playgroud)

我找不到find函数是以不同的名称实现还是已被删除.是否有任何Julia 1.0.2函数与以前的Julia版本中的find函数等效?

Prz*_*fel 7

用途filter():

julia> filter(x -> x<0, -5:5)
5-element Array{Int64,1}:
 -5
 -4
 -3
 -2
 -1
Run Code Online (Sandbox Code Playgroud)

另一种选择是使用findall()获取元素的索引:

julia> indices = findall(x -> x<0, -5:5)
5-element Array{Int64,1}:
 1
 2
 3
 4
 5
Run Code Online (Sandbox Code Playgroud)

您可以使用getindex()获取实际值,例如:

julia> getindex(-5:5,indices)
5-element Array{Int64,1}:
 -5
 -4
 -3
 -2
 -1
Run Code Online (Sandbox Code Playgroud)

  • 最后一个例子也可以简单地写成 `(-5:5)[indices]` 或 `getindex(-5:5,indices)`,因为这里不需要广播。 (2认同)