Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select a subset of an array based on a condition in Julia

Tags:

julia

How do you do simply select a subset of an array based on a condition? I know Julia doesn't use vectorization, but there must be a simple way of doing the following without an ugly looking multi-line for loop

julia> map([1,2,3,4]) do x
       return (x%2==0)?x:nothing
       end
4-element Array{Any,1}:
  nothing
 2
  nothing
 4

Desired output:

[2, 4]

Observed output:

[nothing, 2, nothing, 4]
like image 876
David Parks Avatar asked Nov 27 '22 16:11

David Parks


1 Answers

You are looking for filter http://docs.julialang.org/en/release-0.4/stdlib/collections/#Base.filter

Here is example an filter(x->x%2==0,[1,2,3,5]) #anwers with [2]

like image 158
Lutfullah Tomak Avatar answered Dec 19 '22 21:12

Lutfullah Tomak