Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11684

Re: Count inside a matrix?

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Count inside a matrix?
Date 2012-09-09 09:16 +0000
Message-ID <k2hmq7$pum$1@smc.vnet.net> (permalink)
Organization Time-Warner Telecom

Show all headers | View raw


On 9/8/12 at 3:09 AM, zerge69@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

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Count inside a matrix? Bill Rowe <readnews@sbcglobal.net> - 2012-09-09 09:16 +0000

csiph-web