I am trying to create histograms of randomly generated data, so currently I am doing the following:
Let's say the following array is created:
returns = [0.0024, 0.0231, 0.014, 0.0005, -0.008]
I go through the array using:
returnsRounded[x] = Math.round(returns[x] * 1000) / 1000;
And this will return me:
returnsRounded = [0.002, 0.023, 0.014, 0.001, -0.008]
Now I am trying to count the number of each particular value (in an array of tens of thousands of values, so I very inefficiently will do something akin to:
switch (returnsRounded) {
case -0.040:
arr[1]++;
break;
case -0.038:
arr[2]++;
break;
// ... and so on.
}
And then plug the counted values into my histogram (which works like a bar chart -- in the sense that it only compares counts. I can't just plug in an array and get a histogram.
So is there any more efficient way of counting the number of occurrences of a rounded number in an array that is faster or more pretty than what I'm doing?
Thanks!
Edit: I came up with a much better than my current way, though I don't know if it is still great. Here's a fiddle: