find sum of all the entries with value 0
3 次查看(过去 30 天)
显示 更早的评论
I have a cell matrix of mat = [ 1 0 0 4;0 2 0 4], i want to find the sum of the all the entries having value 0. in the case of given example the entries having count 0 are 4; so sum will be 4. how can i do this.
0 个评论
采纳的回答
更多回答(1 个)
Jos (10584)
2013-11-30
NNZ is the dedicated function to do this:
nnz(~mat)
3 个评论
John D'Errico
2013-11-30
Note that nnz(~mat) will be poor even for large sparse matrices, because if mat is sparse, then ~mat must create a large and rather dense logical matrix. Better then would be to use a form like:
numel(mat) - nnz(mat)
In my tests, for a large fairly sparse matrix like that generated by Matt in his comment, my form was the fastest.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!