Reputation: 8260
Julia appears to have a lot of Matlab like features. I'd like to select from an array using a predicate. In Matlab I can do this like:
>> a = 2:7 ;
>> a > 4
ans =
0 0 0 1 1 1
>> a(a>4)
ans =
5 6 7
I found a kind of clunky seeming way to do part of this in Julia:
julia> a = 2:7
2:7
julia> [int(x > 3) for x in a]
6-element Array{Any,1}:
0
0
1
1
1
1
(Using what wikipedia calls list comprehension). I haven't figured out how to apply a set like this to select with in Julia, but may be barking up the wrong tree. How would one do a predicate selection from an array in Julia?
Upvotes: 36
Views: 37433
Reputation: 1907
11.572 μs ± 25.739 and 8.992 μs ± 15.098 are not so very different even after 10,000 samples. As a quick estimate, this means that the first one would be faster about 47% of the time which is a pretty good synonym for "about equal".
Upvotes: 0
Reputation: 5651
Array comprehension in Julia is somewhat more primitive than list comprehension in Haskell or Python. There are two solutions — you can either use a higher-order filtering function, or use broadcasting operations.
filter(x -> x > 4, a)
This calls the filter
function with the predicate x -> x > 4
(see Anonymous functions in the Julia manual).
a[Bool[a[i] > 4 for i = 1:length(a)]]
This performs a broadcasting comparision between the elements of a
and 4, then uses the resulting array of booleans to index a
. It can be written more compactly using a broadcasting operator:
a[a .> 4]
Upvotes: 21
Reputation: 264
I would like to add an aspect that has not been covered by the previous answers. If you want to filter the array by the index values (as opposed to the array values) you can do so bya[1:end ...]
where instead of the dots you apply a broadcast operator to the index values. E.g. in order to remove the third element you would write
a[1:end .!= 3].
Upvotes: 3
Reputation: 121
I'm currently using Julia 1.3.1 and some syntax has changed compared to earlier answers. To filter an array on multiple conditions I had to do:
x = range(0,1,length=100)
x[(x .> 0.4) .& (x .< 0.51)]
note the '.&' needed to do the AND operator.
Upvotes: 8
Reputation: 19102
To filter the keys in a dictionary, this worked for me:
mydict = Dict("key1" => 1.0, "key2" => 2.0, "a big string with a part of a string" => 3.0)
filter(x -> occursin("part of a string", string(x)), keys(mydict))
Here is what the output looks like on the REPL in Julia 1.0
julia> mydict = Dict("key1" => 1.0, "key2" => 2.0, "a big string with a part of a string" => 3.0)
Dict{String,Float64} with 3 entries:
"key2" => 2.0
"key1" => 1.0
"a big string with a part of a string" => 3.0
julia> filter(x -> occursin("part of a string", string(x)), keys(mydict))
Set(["a big string with a part of a string"])
This in general is a great way to filter an array of strings.
Hope that helps.
Upvotes: 1
Reputation: 353009
You can use a very Matlab-like syntax if you use a dot .
for elementwise comparison:
julia> a = 2:7
2:7
julia> a .> 4
6-element BitArray{1}:
false
false
false
true
true
true
julia> a[a .> 4]
3-element Array{Int32,1}:
5
6
7
Alternatively, you can call filter
if you want a more functional predicate approach:
julia> filter(x -> x > 4, a)
3-element Array{Int32,1}:
5
6
7
Upvotes: 46