I have a 150 by 300 binary matrix. I would like to find the total amount of zeros in the matrix. I've been consulting online documentation for support, but am unable to use the "count" function properly (I only see how to apply it to a list). Perhaps there is also a better way to do this than using count. When providing a solution, I'd appreciate if you can state what the code is doing, so I can better understand. For the sake of simplicity, I will paste a smaller matrix (5 by 10), but the one I will be applying it to will be 150 by 300. Thanks in advance for your time.
binarym = ({
{1, 1, 1, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 1, 0, 0, 0, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0, 1, 1}})
Plus@@Flatten[binarym]
should work. There may be faster ways usingTrace
or similar. Please note that your outer round parentheses do not actually do anything. $\endgroup$ – Yves Klett Jan 2 '18 at 3:12Count
can take a level specification (say 2, for a matrix):Count[binarym, 0, 2]
, while something likeTotal[1 - binarym, 2]
might be faster in some cases. $\endgroup$ – ilian Jan 2 '18 at 3:12Length@Select[Flatten[binarym], # == 0 &]
This gives 17, which is the expected answer. $\endgroup$ – bill s Jan 2 '18 at 3:14Times @@ Dimensions[binarym] - Total@Flatten@binarym
$\endgroup$ – Bob Hanlon Jan 2 '18 at 3:25