Path: news.mathworks.com!newsfeed-00.mathworks.com!kanaga.switch.ch!switch.ch!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.straub-nv.de!weretis.net!feeder2.news.weretis.net!feeder.eternal-september.org!eternal-september.org!not-for-mail
From: dpb <none@non.net>
Newsgroups: comp.soft-sys.matlab
Subject: Re: count number of 1's in the matrix
Date: Tue, 30 Jun 2009 15:47:01 -0500
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <h2dtvu$qt1$1@news.eternal-september.org>
References: <h2dspt$pgv$1@fred.mathworks.com> <cd387b6b-ba68-4100-8218-a03d69c844de@k15g2000yqc.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.eternal-september.org U2FsdGVkX1+Exsd6Y4jhSxK9VZAL4lbU6RCJzFZPCdZnYduevMM/Y4O0o0ei5gDQukCOKSVL9ft9XoIGMaqFCNyIGt+XcbPNItgFbNt4YMQgiNbd8NMrzHkysDb1QBdU72zJqVSOVPc1yExbNpWkKg==
X-Complaints-To: abuse@eternal-september.org
NNTP-Posting-Date: Tue, 30 Jun 2009 20:53:19 +0000 (UTC)
In-Reply-To: <cd387b6b-ba68-4100-8218-a03d69c844de@k15g2000yqc.googlegroups.com>
X-Auth-Sender: U2FsdGVkX189Oe6pdgbnNe0NvUVBKW+WgUxGYYyvR+w=
Cancel-Lock: sha1:lMZmEgc8iwJ9xCkH05k7QeZS4G8=
User-Agent: Thunderbird 2.0.0.21 (Windows/20090302)
Xref: news.mathworks.com comp.soft-sys.matlab:551828


Nathan wrote:
> On Jun 30, 1:33 pm, "Diego Lass" <dlISC...@gmail.com> wrote:
>> Hi
>> I have a matrix with 0's, 1's.  I want to count the number of 1's.
>> A = [1 0 0 0 1 0; 1 0 0 0 1 1; 0 0 0 0 1 1]
>>
>> What is the most efficient way of doing this?
>> Thanks
>> Diego
> 
> I'd just do:
> count = sum(sum(A));

Which works as long as A == 0|1

length(A(A==1))

works elsewhere as well.

--