matlab计算矩阵每列非0元素个数

在统计分析中,有时候需要计算矩阵每列非0元素的个数,可以用以下方法:

先用find找到每列不为0的元素index,然后用count计数。

假设有矩阵A[M,N], 结果存在countZeros

countZeros=zeros(1,N);

for i=1:M

     countZeros(i)=length(find(A(:,i)>0);

end

posted @ 2013-10-15 09:05  huangshanshan  阅读(4263)  评论(0编辑  收藏  举报