This seems like a very newby question, since I’m trying to do something very simple.
I want to find the array elements between two values. I can make it work with my third attempt in the code, but it feels like there is a much more elegant way … .
# attempt 1
findall(a.>b && a.<c) #does not work since bit arrays and boolean operations can't be combined
# attempt 2
findall(comp1 && comp2) # does not work
findall(comp1. && comp2) # does not work
# attempt 3
You can broadcast
& but not
&&, but mind the brackets else you get bitwise
& of the integers. But you can also write two comparisons at once,
b < a < c:
julia> a,b,c = 0:2:50, 10, 20; # no need to collect
julia> findall(b .< a .< c) # indices
julia> a[@. (a>b) & (a<c)] # or a[ans], values
julia> (a .> b .& a .< c) == (a .> (b .& a) .< c) # bitwise on Int
You could also write
filter(x -> b<x<c, a) for the values, or
findall(i -> b<a[i]<c, eachindex(a)) for the indices.
There’s probably a better way, but you can elementwise multiply the bit array, so
findall((a.>b) .* (a.<c))
will give you a bit array for all the indices of the elements you’re looking for. I think you actually want
a[findall((a.>b) .* (a.<c))]
to give the elements.
great!, still a lot to learn converting to Julia
Just a note - the function
findall returns indices of elements that are
true when only given an iterable, or indices of element for which a first-argument function returns
true when given a function and then an iterable. If you want the elements themselves, use the
filter function, optionally with a
! if you want to mutate the iterable (
a in the original post) that is passed.
filter takes a function and then an iterable. The way I would do this is
filter(x -> b<x<c, a)