Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: Counting nonzeros

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86878] Re: [mg86865] Counting nonzeros
  • From: "Thomas Dowling" <thomasgdowling at gmail.com>
  • Date: Tue, 25 Mar 2008 01:14:41 -0500 (EST)
  • References: <200803240644.BAA17004@smc.vnet.net>

Hello,

Using Count and Except[0] is surprisingly slow

listi = RandomInteger[{0, 100}, 100000];

Length[listi] - Count[listi, 0] // Timing

Out[30]= {0.02711, 98995}

Count[listi, Except[0]] // Timing

Out[31]= {0.063781, 98995}

However, I remember reading in a previous post (which I cannot find) about
Tally & Clip, perhaps by Carl Woll

Tally[Clip[listi]][[1, 2]] // Timing

Out[32]= {0.004693, 98995}

I suppose there are even faster ways.

Tom Dowling


On Mon, Mar 24, 2008 at 6: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]
>
>


  • Prev by Date: Tally
  • Next by Date: Re: Possible values for the Method option for Graphics??
  • Previous by thread: Re: Counting nonzeros
  • Next by thread: Re: Counting nonzeros