MathGroup Archive 2008

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

Search the Archive

Re: Counting nonzeros

  • To: mathgroup at
  • Subject: [mg86873] Re: Counting nonzeros
  • From: Szabolcs Horvát <szhorvat at>
  • Date: Tue, 25 Mar 2008 01:13:43 -0500 (EST)
  • Organization: University of Bergen
  • References: <fs7iml$gma$>

carlos at wrote:
> I want to count the # of NZ entries in an arbitrary multilevel list,
> say exp, that contains only integers.  Is this the easiest way:
>   k = Length[Flatten[exp]]-Count[Flatten[exp],0]

Suppose that we only have integers in the list.

Count[expr, Except[0, _Integer], Infinity]

Instead of _Integer, we could use _Symbol or _?AtomQ or some other 
pattern depending on the type of elements that need to be counted.

(_Integer was necessary to avoid counting all the compound expressions, 
such as sublists.)

  • Prev by Date: Re: Re: Re: Re: Re: smallest
  • Next by Date: Tally
  • Previous by thread: Re: Counting nonzeros
  • Next by thread: Re: Counting nonzeros