In my code (Python+numpy or Matlab, but issue can apply to any array programming language), I often need to count the number of elements in an array where the elements are true. Usually, I implement this as sum(x>0.5)
, but this may be considered improper. See Is it correct to add booleans in order to count the number of true values in a vector?. Numpy has count_nonzero
, but if there is no special-purpose function, what would be the proper way to write this?
For example, in Matlab
, I can either write sum(x>.5)
or length(find(x>0.5))
. The former is slightly faster but may be considered improper (see above). Are there any other alternatives for counting the number of true values in an array, and what should be my criteria for selecting one?
(In a low-level language, one would write an explicit for-loop, but that is an extremely inefficient way to implement this in a high-level array programming language)
4
This seems like a natural use for a fold to me
i.e. something like this in pseudo functional code
let countiftrue bs = fold (fun acc b -> if b then acc + 1 else acc) 0 bs
better yet have it as a higher order function that accepts a predicate to make it even more useful
let countif p xs = fold (fun acc x -> if p x then acc + 1 else acc) 0 xs
It seems like if you want to count the true elements, you probably want to get the true elements in some other context. Also, getting the true elements and then counting them reads nicely, in my opinion.
My python is rusty, but this will give you the general idea:
def take_true(list):
return [x for x in list if x]
def true_count(list):
return len(take_true(list))
1
In Python you can use generator expressions with many builtin functions. sum(x for x in vec if x > 0.5)
and len(x for x in vec if abs(x) > 1.0)
are perfectly valuable. They’re equivalent to using a list comprehension like sum([x for x in vec if x > 0.5])
but doesn’t have the overhead of needing to building a temporary list.
You can extend this idea by looking into map, filter & reduce (as well as the itertools versions that more efficiently handle iterators).
2