Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: count number of 1's in the matrix
Date: Tue, 30 Jun 2009 21:46:01 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 26
Message-ID: <h2e12p$a01$1@fred.mathworks.com>
References: <h2dspt$pgv$1@fred.mathworks.com> <op.uwcwssi0a5ziv5@uthamaa.dhcp.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246398361 10241 172.30.248.37 (30 Jun 2009 21:46:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 30 Jun 2009 21:46:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:551849


"Ashish Uthama" <first.last@mathworks.com> wrote in message <op.uwcwssi0a5ziv5@uthamaa.dhcp.mathworks.com>...
> On Tue, 30 Jun 2009 16:53:01 -0400, Bruno Luong  
> <b.luong@fogale.findmycountry> wrote:
> 
> > nnz(A)
> >
> > Bruno
> 
> %as noted earlier, SUM works only for 1/0 matrices
> 
> >> d=double(rand(5010)>.5);
> >> timeit(@()nnz(d))
> 
> ans =
> 
>        0.13992
> 
> >> timeit(@()sum(sum(d)))
> 
> ans =
> 
>       0.031067

If one want speed, then sum(d(:)) is probably the fastest.

Bruno