Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Count occurences by row
Date: Fri, 16 Jul 2010 18:17:20 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 13
Message-ID: <i1q7ng$78g$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1279304240 7440 172.30.248.35 (16 Jul 2010 18:17:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 16 Jul 2010 18:17:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2299192
Xref: news.mathworks.com comp.soft-sys.matlab:653755

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.