Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Count occurences by row
Date: Fri, 16 Jul 2010 18:45:20 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 35
Message-ID: <i1q9c0$li7$1@fred.mathworks.com>
References: <i1q7ng$78g$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1279305920 22087 172.30.248.37 (16 Jul 2010 18:45:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 16 Jul 2010 18:45:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:653761

"Namo Namo" <wynamo@yahoo.com> wrote in message <i1q7ng$78g$1@fred.mathworks.com>...
> Say I have a matrix
> 
> a = [ 1 2 2
>   2 3 3
>   1 4 5 ];
> 
> I want to count the occurences of 1 2 ... 5, but by how many rows.  That is, although 2 appears 3 times, but only in 2 rows.  I can count the total times by tabulate(a(:)) or histc, accumarray, etc.  But to count the total rows, I am doing a for loop
> 
> for i = 1:N
> count(i) = sum( any( a==i, 2) )
> end
> 
> any advice on how to speep up for large N?  Thanks.

one of the solutions

% the data
     a=[
          1     2     2
          2     3     3
          1     4     5
     ];
% the engine
     nx=1:max(a(:));
     n=histc(a.',nx);
     n=sum(n~=0,2);
% the result
     disp([nx;n.']);
%{
          1     2     3     4     5     % <= unique val...
          2     2     1     1     1     % <- #rows
%}

us