MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Count inside a matrix?


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?