Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Function to Count Number of true elements in a boolean matrix

Subject: Function to Count Number of true elements in a boolean matrix

From: Shaddy

Date: 16 Jan, 2009 21:46:01

Message: 1 of 4

Hi All,

I'm wondering if there's a function out there to count the number of true values in a boolean matrix. I know that the sum function does the trick (even though the sum of a number of true values doesn't really make sense), but I assume that this is rather slow and there would be a function out there that is optimized for use on boolean matrices.

Thanks,

Subject: Function to Count Number of true elements in a boolean matrix

From: Roger Stafford

Date: 16 Jan, 2009 21:57:02

Message: 2 of 4

"Shaddy " <s.shokrallaREMOVETHIS@gmail.com> wrote in message <gkqv6p$70h$1@fred.mathworks.com>...
> Hi All,
>
> I'm wondering if there's a function out there to count the number of true values in a boolean matrix. I know that the sum function does the trick (even though the sum of a number of true values doesn't really make sense), but I assume that this is rather slow and there would be a function out there that is optimized for use on boolean matrices.
>
> Thanks,

  You could try such things as numel(b(b)) with b the boolean array, but I doubt if you can beat the 'sum'.

Roger Stafford

Subject: Function to Count Number of true elements in a boolean matrix

From: Rajiv Singh

Date: 16 Jan, 2009 22:00:26

Message: 3 of 4

You may try things like find(x(x),1,'last'), or numel(x(x)), but sum(x(:))
is faster.

-rajiv

"Shaddy " <s.shokrallaREMOVETHIS@gmail.com> wrote in message
news:gkqv6p$70h$1@fred.mathworks.com...
> Hi All,
>
> I'm wondering if there's a function out there to count the number of true
> values in a boolean matrix. I know that the sum function does the trick
> (even though the sum of a number of true values doesn't really make
> sense), but I assume that this is rather slow and there would be a
> function out there that is optimized for use on boolean matrices.
>
> Thanks,

Subject: Function to Count Number of true elements in a boolean matrix

From: Steven Lord

Date: 16 Jan, 2009 22:30:21

Message: 4 of 4


"Shaddy " <s.shokrallaREMOVETHIS@gmail.com> wrote in message
news:gkqv6p$70h$1@fred.mathworks.com...
> Hi All,
>
> I'm wondering if there's a function out there to count the number of true
> values in a boolean matrix. I know that the sum function does the trick
> (even though the sum of a number of true values doesn't really make
> sense), but I assume that this is rather slow and there would be a
> function out there that is optimized for use on boolean matrices.

In addition to the other suggestions, you might want to use NNZ,
particularly if your "boolean" matrix is sparse.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

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.

Contact us