4
$\begingroup$

I want to count the number of numbers lying between 0 and -1 in a vector called spec.

I have tried Count[spec,spec<0 && spec>-1], but this does not work. There must be an easy answer...

$\endgroup$
1
  • $\begingroup$ Old duplicate on Stack Overflow: (6026827). Related: (9637) $\endgroup$
    – Mr.Wizard
    Commented Jul 1, 2017 at 14:13

5 Answers 5

10
$\begingroup$
SeedRandom[1]
spec = RandomReal[{-2, 2}, 100];

Tr[(1 - UnitStep[spec]) UnitStep[spec -(-1)]]
Tr@Unitize@Clip[spec, {-1, 0}, {0, 0}]
Count[spec, _?(-1 <= # <= 0 &)]

all give

24

Some timings:

SeedRandom[1]
s = RandomReal[{-2, 2}, 10^6]; 
Grid[{#, ClearSystemCache[]; AbsoluteTiming[#[s]]} & /@ {
  Length@ Pick[#, Sign@Clip[#, {-1, 0}, {1, 1}], -1] &,
  Tr[(1 - UnitStep[#]) UnitStep[# - (-1)]] &,
  Tr@Unitize@Clip[#, {-1, 0}, {0, 0}] &,
  Count[#, _?(-1 <= # <= 0 &)] &,
  Total[Boole[-1 <= # <= 0] & /@ #] &,
  Length@Select[#, And[# > -1, # < 0] &] &,
  Length@Cases[#, x_ /; -1 < x < 0] &} ]

Mathematica graphics

For SeedRandom[1];s = RandomReal[{-2, 2}, 10^7]; we get:

Mathematica graphics

Note: Still on version 9, so cannot include CountsBy in the timings table.

$\endgroup$
3
  • $\begingroup$ Great, but why is the condition syntax different from Select []? $\endgroup$
    – Morgan
    Commented Jun 24, 2017 at 12:00
  • $\begingroup$ @Morgan, The second argument of Count is a pattern (see Count), while the second argument of Select is a boolean function (see Select) $\endgroup$
    – kglr
    Commented Jun 24, 2017 at 12:12
  • 1
    $\begingroup$ @Morgan see (18054), (88220) $\endgroup$
    – Mr.Wizard
    Commented Jul 1, 2017 at 14:49
4
$\begingroup$
Total[Boole[-1 <= # <= 0] & /@ spec]
$\endgroup$
2
  • $\begingroup$ Isn't Unitize redundant? $\endgroup$
    – Coolwater
    Commented Jun 24, 2017 at 11:33
  • $\begingroup$ @Coolwater d'oh...I have amended. I had an answer that used Unitize and didn't think. Thanks :) $\endgroup$
    – ubpdqn
    Commented Jun 24, 2017 at 11:36
3
$\begingroup$
SeedRandom[1]
spec = RandomReal[{-2, 2}, 100];

CountsBy[spec, -1 <= # <= 0 &]

enter image description here

CountsBy[spec, -1 <= # <= 0 &][True]

24

Update

This one is very fast:

s = RandomReal[{-2, 2}, 10^6];

Length @ Pick[s, Sign @ Clip[s, {-1, 0}, {1, 1}], -1] // AbsoluteTiming

{0.032002, 249536}

$\endgroup$
1
$\begingroup$
Select[spec, And[# > -1, # < 0] &] // Length
$\endgroup$
2
  • 1
    $\begingroup$ For what it's worth, if you change it to Select[spec, -1 < # < 0 &] // Length it gets considerably faster on large lists. $\endgroup$ Commented Jun 24, 2017 at 10:27
  • $\begingroup$ Nice, thank you. $\endgroup$
    – Morgan
    Commented Jun 24, 2017 at 10:28
1
$\begingroup$
SeedRandom[1];
spec = RandomReal[{-2, 2}, 100000];

Length@Cases[spec, x_ /; -1 < x < 0]

25288

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.