S -
S -

Reputation: 408

Find function in Julia 1.0.2

I am transitioning to Julia 1.0.2 and I realized that the find function is not defined. In a previous version (Julia 0.6) I could write

find(x -> x<0, my_var)

In order to get the negative elements of the array called my_var. When I run the same code in Julia 1.0.2 I get the following error:

UndefVarError: find not defined

I couldn't find whether the find function is implemented under a different name or if it has been dropped. Is there any Julia 1.0.2 function that would be equivalent to the find function in previous Julia versions?

Upvotes: 10

Views: 10392

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42264

Use filter():

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

Another option is to use findall() to get the indices of elements:

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

You can use getindex() to get the actual values, e.g.:

julia> getindex(-5:5,indices)
5-element Array{Int64,1}:
 -5
 -4
 -3
 -2
 -1

Upvotes: 11

Related Questions