Archive for November, 2013

Index of elements in array that fulfils a condition in Julia

In Julia (julialang.org) if you are rather interested in finding the indices (indexes) of the elements in an array that fulfils a certain boolean condition then find is your friend, like so:


julia> probs = [0.1, 0.2, 0.3, 0.4, 0.5]
5-element Array{Float64,1}:
0.1
0.2
0.3
0.4
0.5

julia> find(x->x>0.2, probs)
3-element Array{Int64,1}:
3
4
5

Conditional Array indexing in Julia

Conditional array indexing is a really nice convenience when one wants to access array items that fullfills a certain boolean criteria. This is done in Julia in the following way:


julia> probs = [0.1, 0.2, 0.3, 0.4, 0.5]
5-element Array{Float64,1}:
0.1
0.2
0.3
0.4
0.5

julia> probs[probs.>0.2]
3-element Array{Float64,1}:
0.3
0.4
0.5

Observe the “.” in the .> operator which signifies elemetwise comparison.
If you omit the “.” you will get the following error:


probs[probs>0.2]
ERROR: no method isless(Float64,Array{Float64,1})
in > at operators.jl:19

Tags:

UTF-8 and Unicode FAQ

From Evernote Note
via IFTTT

Tags: