how to count number of zeros in each row and column?
3 次查看(过去 30 天)
显示 更早的评论
how can count the number of zeros in each row and in each column like this
A = [ 1 1 0 1 0 1
0 0 1 0 1 0
1 1 0 0 0 1
0 0 1 0 1 0]
B = [ 2
4
3
4 ]
C = [ 2 2 2 3 2 2 ]
0 个评论
回答(1 个)
dpb
2016-4-9
编辑:dpb
2016-4-10
You're not tryin'...
>> sum(~A,2)
ans =
2
4
3
4
>> sum(A==0)
ans =
2 2 2 3 2 2
>>
NB: The latter if A is something other than [0,1]. Showed two alternatives for variety, there are a number of other, particularly when using the special characteristics of the specific values given.
ADDENDUM Actually, ~A is safe for Matlab irrespective of the values of A as long as exact zero is the criterion (that is, no "approximately equal" small floating point values) as anything nonzero is treated as T. This isn't so in some other languages.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!