MathGroup Archive 2008

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

Search the Archive

Re: Counting nonzeros

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86887] Re: Counting nonzeros
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Tue, 25 Mar 2008 01:16:23 -0500 (EST)

On 3/24/08 at 1:44 AM, carlos at Colorado.EDU 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]

There are a number of ways to do what you want. I think using
Unitize to convert the non-zero entries to 1 and then summing
using Total is likely to be the fastest, i.e.,

Total[Flatten@Unitize@exp]

or

Total[Total@Unitize@exp]


  • Prev by Date: Re: Counting nonzeros
  • Next by Date: Hide a PopupMenu in Manipulate
  • Previous by thread: Re: Counting nonzeros
  • Next by thread: Re: Counting nonzeros