Is it correct to add booleans in order to count the number of true values in a vector?
Posted
by
gerrit
on Programmers
See other posts from Programmers
or by gerrit
Published on 2012-10-19T22:36:52Z
Indexed on
2012/10/19
23:19 UTC
Read the original article
Hit count: 193
design
Is it conceptually correct to sum a vector of booleans? From a mathematical point of view, I would argue it's not: True + True != 2
. But it's quite practical to do so still! Example using the vectorised Python
library numpy
:
In [1]: X = rand(10)
In [2]: large = X>0.6
In [3]: large.dtype
Out[3]: dtype('bool')
In [4]: large.sum()
Out[4]: 7
I don't like it, but it's very practical. Is this a good practice?
Update: the aim is to count the number of true values in a vector.
© Programmers or respective owner