Timeline for More efficient way of counting the number of values within an interval?
Current License: CC BY-SA 4.0
13 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 5 at 10:14 | comment | added | Damir Tenishev | @ChrisWue, thank you for the update. I applied the same workaround in my code (when I met the problem). On your comment on workaround; of course, it is a kind of personal sense,but I consider conditional expressions (even for boundary conditions) as workarounds, especially when they come in the expensive loops (hard to read, takes extra time to execute). The code doesn't contain workarounds when it is transparent to any special cases. In case this would be incorporated into the calculations before the loop or disappear at all by changing the algorithm, I wouldn't consider this as a workaround. | |
Sep 5 at 4:34 | comment | added | greybeard | (Defensive coding would add counters for values above&below and need to special-case upper bound.) | |
Sep 5 at 1:51 | comment | added | ChrisWue | @DamirTenishev Thanks, I've updated the answer with a fix. Not sure if you consider that as a work-around. The other option would be to extend the number of buckets by 1. | |
Sep 5 at 1:50 | history | edited | ChrisWue | CC BY-SA 4.0 |
corrected reported bug
|
Aug 15 at 18:47 | comment | added | Damir Tenishev | @michar, this "small note" makes the code above wrong and buggy with hard to catch out-of-range bug; funny for accepted and many times upvoted code. Anyway, are you aware of good examples of the fix you have mentioned? All ways I can see so far are workaround-style and I don't like them. | |
Nov 20, 2020 at 1:25 | comment | added | michar | A small note: it's always tricky with histograms on a closed interval like [0 1] - where the upper bound will get mapped beyond the number of available buckets. In the example above, an area value of 1.0 will get mapped to bucket index 20 (1.0/.05), but you've only allocated 20 buckets (indexes 0 to 19). A common thing is just to include the upper end of the range into the last bucket... | |
Jan 11, 2014 at 2:51 | vote | accept | aries0152 | ||
Jan 10, 2014 at 17:56 | comment | added | ChrisWue |
@aries0152: Well, that would be an off-topic question here (we only review working code) but you print out the bucket_size and the number_of_buckets which are fixed. What you need to print is: for (std::vector<int>::size_type i = 0; i < histogram.size(); ++i) { cout << (i + 1) * bucket_size << "\t" << histogram[i] << "\n"; }
|
|
Jan 10, 2014 at 12:56 | comment | added | aries0152 | @ChrisWue This is what I was looking for. But I am having trouble using it (may be some noobie mistakes). I have updated my post and added the new code (based on your answer). Please check. Could you please help me? | |
Jan 10, 2014 at 3:25 | comment | added | ChrisWue | @MichaelUrman: Yes, the code operates on the assumption stated above :). In real life you certainly need input checking. | |
Jan 10, 2014 at 3:23 | comment | added | Michael Urman |
I love how this transforms the problem to something much more manageable. However as it involves reading input, I would strongly suggest putting some sort of range checks either on area or on bucket before using it to subscript histogram . I presume this was omitted for clarity of answer. :)
|
|
Jan 9, 2014 at 19:32 | history | edited | Jamal | CC BY-SA 3.0 |
added 3 characters in body
|
Jan 9, 2014 at 19:14 | history | answered | ChrisWue | CC BY-SA 3.0 |