7
$\begingroup$

I have a list of numbers which I have rounded and now all numbers are divisible by 5.

    napajecky=Round[{0.227`, 0.155`, 0.184`, 0.178`, 0.248`, 0.106`, 0.295`, 0.286`, 0.126`, 0.109`, 0.172`, 0.364`, 0.131`, 0.268`, 0.3`, 0.172`, 
   0.324`, 0.359`, 0.52`, 0.277`, 0.169`, 0.303`, 0.169`, 0.097`, 
   0.265`, 0.063`, 0.262`, 0.19`, 0.095`, 0.059`, 0.035`, 0.051`, 
   0.163`, 0.099`, 0.038`, 0.093`, 0.16`, 0.1454`, 0.3803`, 0.2437`, 
   0.0359`, 0.0959`, 0.0346`, 0.136`, 0.275`, 0.003`, 0.004`, 0.003`, 
   0.003`, 0.005`, 0.1686`, 0.1031`}*100, 5]

Now, I need to make a new list which would count number of occurences of numbers divisible by 5 in the rounded list and link it with that particular number. So the new list would look like this e.g.: newlist={number of occurence of number 5 is 7, number of occurences of number 10 is 8,number of occurences of number 15 is 12, ... etc.}

I tried to define function that would count these occurences in certain range (5 to 80) but it does´t work:

f[x_]:=Count[x,Range[5,80,5]
res=f/@napajecky

Can you help me to find the best way? Thank you for advice.

$\endgroup$
  • 1
    $\begingroup$ BinCounts and Counts would be useful here. $\endgroup$ – Roman Aug 7 at 13:16
  • $\begingroup$ See Tally which is included in the See Also section of the documentation for Count $\endgroup$ – Bob Hanlon Aug 7 at 13:57
8
$\begingroup$
BinCounts[napajecky, {0,85,5}]
(*    {5, 7, 8, 12, 3, 6, 7, 2, 1, 0, 1, 0, 0, 0, 0, 0, 0}    *)

Notice that the range goes to 85 so that the last bin is $[80,85)$ (from 80 inclusive to 85 exclusive).

Alternatively you could bin directly, without rounding first:

napajecky = {0.227, 0.155, 0.184, 0.178, 0.248, 0.106, 0.295, 0.286,
             0.126, 0.109, 0.172, 0.364, 0.131, 0.268, 0.3, 0.172, 0.324,
             0.359, 0.52, 0.277, 0.169, 0.303, 0.169, 0.097, 0.265, 0.063, 
             0.262, 0.19, 0.095, 0.059, 0.035, 0.051, 0.163, 0.099, 0.038,
             0.093, 0.16, 0.1454, 0.3803, 0.2437, 0.0359, 0.0959, 0.0346,
             0.136, 0.275, 0.003, 0.004, 0.003, 0.003, 0.005, 0.1686, 0.1031};

BinCounts[napajecky, {-0.025, 0.825, 0.05}]
(*    {5, 7, 8, 12, 3, 6, 7, 2, 1, 0, 1, 0, 0, 0, 0, 0, 0}    *)
$\endgroup$
7
$\begingroup$
res = Lookup[Counts[napajecky], Range[5, 80, 5], 0]

{7, 8, 12, 3, 6, 7, 2, 1, 0, 1, 0, 0, 0, 0, 0, 0}

$\endgroup$
4
$\begingroup$

For completeness' sake, Tally:

Range[5, 80, 5] /. Append[Rule @@@ Tally[napajecky], _Integer -> 0]

{7, 8, 12, 3, 6, 7, 2, 1, 0, 1, 0, 0, 0, 0, 0, 0}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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