Re: Count inside a matrix?

• To: mathgroup at smc.vnet.net
• Subject: [mg128018] Re: Count inside a matrix?
• From: Bill Rowe <readnews at sbcglobal.net>
• Date: Sun, 9 Sep 2012 05:09:46 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-newout@smc.vnet.net
• Delivered-to: mathgroup-newsend@smc.vnet.net

```On 9/8/12 at 3:09 AM, zerge69 at gmail.com (Sergio Sergio) wrote:

>Hi, does anybody know how to count elements within a matrix? I know
>how that Count works for lists, but it does not seem to work for a
>matrix. Any tips?

It would be easier to make useful suggestions if you would
provide more details of what you want to do. Meanwhile, here are
some ideas:

use the third argument of Count,

In[1]:= m = RandomInteger[10, {20, 20}];

In[2]:= Count[m, 2, 2]

Out[2]= 32

Use Flatten with Count

In[1]:= m = RandomInteger[10, {20, 20}];

In[2]:= Count[m, 2, 2]

Out[2]= 32

Use Total with Count

In[4]:= Total[Count[#, 2] & /@ m]

Out[4]= 32

Or for something that doesn't use pattern matching and is likely
much faster for some applications

In[5]:= Total[1 - Unitize[m - 2], 2]

Out[5]= 32

```

• Prev by Date: Re: Transforming/expanding a list
• Next by Date: Re: Sum command
• Previous by thread: Re: Count inside a matrix?
• Next by thread: Re:Count inside a matrix?