count number of ones in binary matrix
20 次查看(过去 30 天)
显示 更早的评论
1 0 1
0 1
1 1 0
i need the output as : no of ones is 5
0 个评论
回答(5 个)
James Tursa
2018-1-20
Assuming you mean the sum of all elements:
A = your matrix;
result = sum(A(:));
If you really mean just the edges or something else, let us know.
0 个评论
Image Analyst
2018-1-20
Do you really only care about the outer perimeter? So if the center is a 1, you want to ignore that? If so:
mTemp = m; % Make copy
mTemp(2, 2) = 0; % Make center zero so we won't count it if it's a 1.
numZeros = nnz(mTemp); % Effectively, count 1's in outer perimeter only.
0 个评论
sumanth kumar
2020-2-2
function y = one(x)
c=0;
for i=1:length(x)
if(x(i)==49)
c=c+1;
end
end
y=c;
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!