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....
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With