필터 지우기
필터 지우기

how to count number of zeros in each row and column?

조회 수: 3 (최근 30일)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 9일
편집: dpb 2016년 4월 10일
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 ]

답변 (1개)

dpb
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.
  댓글 수: 4
Walter Roberson
Walter Roberson 2016년 4월 10일
function [B, C] = nnz_function(A)
B = sum(~A,2);
C = sum(~A);
dpb
dpb 2016년 4월 10일
Wonder if would be worth having optional second arg of DIM for nnz??

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by