Thread Subject: count number of 1's in the matrix

Subject: count number of 1's in the matrix

From: Diego Lass

Date: 30 Jun, 2009 20:33:01

Message: 1 of 7

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

Subject: count number of 1's in the matrix

From: Nathan

Date: 30 Jun, 2009 20:38:07

Message: 2 of 7

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));

Subject: count number of 1's in the matrix

From: dpb

Date: 30 Jun, 2009 20:47:01

Message: 3 of 7

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.

--

Subject: count number of 1's in the matrix

From: Bruno Luong

Date: 30 Jun, 2009 20:53:01

Message: 4 of 7

nnz(A)

Bruno

Subject: count number of 1's in the matrix

From: Ashish Uthama

Date: 30 Jun, 2009 21:32:42

Message: 5 of 7

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

Subject: count number of 1's in the matrix

From: Bruno Luong

Date: 30 Jun, 2009 21:46:01

Message: 6 of 7

"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

Subject: count number of 1's in the matrix

From: Bruno Luong

Date: 30 Jun, 2009 22:25:03

Message: 7 of 7

>> d=double(rand(5010)>.5);
>> timeit(@()nnz(d))

ans =

    0.1583

>> timeit(@()sum(d(:)))

ans =

    0.0408

>> timeit(@()sum(sum(d)))

ans =

    0.0409

>>

>> d=sparse(d);
>> timeit(@()nnz(d))

ans =

  2.0785e-005

>> timeit(@()sum(d(:)))

ans =

    0.2306

>> timeit(@()sum(sum(d)))

ans =

    0.0209

% Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix manipula... Diego Lass 30 Jun, 2009 16:34:03
rssFeed for this Thread

Contact us at files@mathworks.com