35

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?

Peeter Joot
  • 7,848
  • 7
  • 48
  • 82

5 Answers5

45

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
DSM
  • 342,061
  • 65
  • 592
  • 494
21

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.

Higher-order filtering

filter(x -> x > 4, a)

This calls the filter function with the predicate x -> x > 4 (see Anonymous functions in the Julia manual).

Broadcasting and indexing

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]
Peeter Joot
  • 7,848
  • 7
  • 48
  • 82
jch
  • 5,382
  • 22
  • 41
  • 1
    I have an array and would like to filter it on more than one thing. For example a > 3 && a < 5. However when I try to do this I get the error that non-boolean (BitArray{1}) used in boolean context. What is the problem here? – lara Apr 21 '16 at 23:37
  • This works fine with `filter`: `filter( x -> (x > 3 && x < 5), a)` http://docs.julialang.org/en/latest/stdlib/collections/?highlight=filter#Base.filter – Fred Schoen May 03 '16 at 08:50
  • 1
    And for arrays, you have to use bitwise operators `a .> 3 & a .< 5` Notice the single `&` for `bitwise and` – Fred Schoen May 03 '16 at 08:59
  • 2
    Note that `a=[1; 2; 3]; a[a.>0 & a.<1]` will not yield the expected result! This gives `1 2 3`, as the `&` operator has higher precedence. A correct solution is `a[(a.>0) & (a.<1)]`. – esel Jul 13 '16 at 11:49
8

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.

Alexander Kooij
  • 121
  • 1
  • 3
3

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].
KloppyToppy
  • 244
  • 2
  • 5
1

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.

phyatt
  • 18,472
  • 5
  • 61
  • 80