× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Find the number of zero elements in a matrix in MATLAB

Ray Phan
Feb 02, 2015
<p>I'll add something to the mix as well. You can use <a href="http://www.mathworks.com/help/matlab/ref/histc.html" rel="nofollow"><code>histc</code></a> and compute the histogram of the entire matrix. You specify the second parameter to be which bins the numbers should be collected at. If we just want to count the number of zeroes, we can simply specify <code>0</code> as the second parameter. However, if you specify a matrix into <code>histc</code>, it will operate along the columns but we want to operate on the entire matrix. As such, simply transform the matrix into a column vector <code>A(:)</code> and use <code>histc</code>. In other words, do this:</p> <pre><code>histc(A(:), 0) </code></pre> <p>This should be equivalent to counting the number of zeroes in the entire matrix <code>A</code>.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/25321341/Find%20the%20number%20of%20zero%20elements%20in%20a%20matrix%20in%20MATLAB/25321862">Stack Overflow</a>.</p>
comments powered by Disqus