Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to count rows in array that match some condition in Julia

Tags:

arrays

julia

In Julia, I have an array of Booleans like this:

3×5 Array{Bool,2}:
  true  false  true  false   true
  true   true  true   true  false
 false  false  true   true  false

Except I have far more than three rows. What's the fastest/best way to count, say, the number of rows in which the second and fourth column are true, as well as the number of rows in which the second and third column are true? Right now I have something like this, where N is the number of rows:

num_2and3true = 0
num_2and4true = 0
for i = 1:N                                                                                                            
  if m[i,2] == 1                                                                                                        
    if m[i,3] == 1                                                                                                         
      num_2and3true += 1                                                                                                  
    end                                                                                                                  
    if m[i,4] == 1                                                                                                         
      num_2and4true += 1                                                                                                 
    end                                                                                                                
  end                                                                                                                
end 

Is there a faster way to do this? I have a hunch that the way I'm doing it is too simple to be the best way. Thanks....

like image 326
Ben S. Avatar asked Dec 19 '25 20:12

Ben S.


1 Answers

A faster but different approach is to use BitVectors which compress Bool Vectors and enable using single AND instructions to replace 64 logical AND operations. Of course, this comes at a cost of allocating some memory, but it turns out, the benefit outweighs the costs and it comes out faster (caveat emptor, on my machine). The function is:

function bar(m)
    num_2and3true = 0
    num_2and4true = 0
    v2 = BitVector(view(m,:,2))
    v3 = BitVector(view(m,:,3))
    v4 = BitVector(view(m,:,4))
    num_2and3true += sum(v2 .& v3)
    num_2and4true += sum(v2 .& v4)
    return (num_2and3true, num_2and4true)
end
like image 150
Dan Getz Avatar answered Dec 21 '25 08:12

Dan Getz