Count nonzero elements in each row of the matrix
3 answers
A more esoteric version could use accumarray
and bsxfun
with nnz
as a function to apply values ββfor each column / group of the input matrix A
. Not as efficient as using sum
and multiplying a matrix, but still a way to think about :):
B = bsxfun(@times, 1:size(A,1), ones(size(A,2),1)).'; out = accumarray(B(:), A(:), [], @nnz);
0
source to share