Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find function in Julia 1.0.2

Tags:

julia

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?

like image 351
A. A. Avatar asked Nov 13 '18 15:11

A. A.


1 Answers

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
like image 121
Przemyslaw Szufel Avatar answered Nov 08 '22 02:11

Przemyslaw Szufel