MATLAB: Counting number of zeros in column or row, without looping

non-zeros

Hi Everyone,
If I have a matrix A, is there a way to create a 1,c vector containing the number of non-zeros in each columns, and simillarly, a r,1 vector containing the number of non-zeros in each row, all without looping?
Perhaps some use of nnz I am unaware of?
Regards,
Ulrik

Best Answer

Given A:
idx = A~=0;
c = sum(idx,1);
r = sum(idx,2);