How to select elements from array in Julia matching predicate?

27,877

Solution 1

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

Solution 2

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]

Solution 3

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.

Solution 4

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].
Share:
27,877
Peeter Joot
Author by

Peeter Joot

I have worked at LzLabs as a software developer since 2016, where I am responsible for COBOL and PL/I compiler runtime development and compiler integration, and legacy C370 runtime implementation. Before LzLabs, I worked for IBM for close to 20 years, working on the lowest level guts of DB2 LUW. I spend too much of my free time, when I can find it, enjoying recreational mathematics, physics and engineering studies. At http://peeterjoot.com/ you can find my book on geometric algebra applications to electromagnetism, free pdfs of class notes for many of the physics and engineering courses that I have taken, and my musings about math, physics, programming, and other random topics that have caught my interest.

Updated on February 03, 2021

Comments

  • Peeter Joot
    Peeter Joot about 3 years

    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?