Counting ocurences in a list in Mathematica (with code, but not fully working)
My problem is this:
I have a list list
and I want to iterate through this list and count the number of times each values from 0 to 1 occur in this in a step size step
. I have written the following code:
list = {0.2, 0.2, 0.7, 0.8, 0.17};
countingfornormal[list_, step_] := Module[{temp, num, res, i},
temp = Round[list, step];
num = {};
For[i = 0, i <= 1, i += step, {
res = Count[temp, i];
AppendTo[num, {i, res}];
}];
num
]
However, for a step size of 0.01, the output only seems to count the values up to 0.17. It's really frustrating me and I can't seem to figure out why. I have also noted that for a step size of 0.02, the output counts the values up to 0.34.
I round off at the start because originally I was using a larger list with values to around 7 places after the decimal point.开发者_如何学运维
I thought maybe it was a timing issue but I'm unsure how to fix such a thing.
Any help would be greatly appreciated.
Perhaps better:
list = {0.2, 0.2, 0.7, 0.8, 0.17};
b = BinCounts[list, {0, 1, 0.01}]
(*
->{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0}
*)
BarChart@b
精彩评论