Faster array indexing with a logical array

You don’t need findall, you can index with the boolean array from broadcasting .< (as in the python version). But even better is to avoid creating the sum anywhere:

julia> gb1(x) = x[vec(sum(x,dims=2) .< 1), :];

julia> gb2(x) = x[(@views x[:,1] .+ x[:,2] .< 1), :];

julia> @btime gb1($points);
  30.618 μs (13 allocations: 160.41 KiB)

julia> @btime gb2($points);
  20.707 μs (5 allocations: 82.00 KiB)

julia> @btime getindicesVectorized($points); # to compare
  37.253 μs (15 allocations: 275.02 KiB)
3 Likes