alternative rounding method when using histcounts
Mostrar comentarios más antiguos
My understanding is that when using hiscounts matlab uses the "round up" method when a value lies exactly on a bin edge.
Is it possible to implement a different type of rounding startegy e.g. bankers rounding/gaussian rounding? e.g 4.5 would round to 4 not to 5
It doesn;t appear to be an optional parameter to pass to the hiscounts function
Thanks
Oli
2 comentarios
Guillaume
el 14 de Abr. de 2020
I'm not sure what you mean. histcounts doesn't round anything. Can you describe your problem in more details?
Oliver Warlow
el 14 de Abr. de 2020
Respuesta aceptada
Más respuestas (1)
Steven Lord
el 14 de Abr. de 2020
0 votos
If a value in your data exactly matches one of the elements of the edges vector, that value is counted in the right bin of the two (unless it matches the last element of the edges vector, in which case it's in the last bin.) From the histcounts documentation page:
"[N,edges] = histcounts(X,edges) sorts X into bins with the bin edges specified by the vector, edges. The value X(i) is in the kth bin if edges(k) ≤ X(i) < edges(k+1). The last bin also includes the right bin edge, so that it contains X(i) if edges(end-1) ≤ X(i) ≤ edges(end)."
Bins other than the last contain their left edge but not their right, and the last bin contains both edges.
There's no option to change which edge each bin contains (to make the first bin contain both its edges and make all others contain their right edge but not their left.) The discretize function has an option that does this, so asking for a similar option in histcounts and related functions seems to me like a reasonable enhancement request for you to file with Technical Support.
3 comentarios
Oliver Warlow
el 14 de Abr. de 2020
Guillaume
el 14 de Abr. de 2020
"seems to me like a reasonable enhancement request for you to file with Technical Support"
FWIW, I've already submitted that as enhancement request earlier today. It wouldn't hurt Oliver requesting that as well. The more people asking for it, the more chance it gets implemented.
The rounding business, I'm not so sure it would get implemented though. As I've said already, there's no rounding happening at all. Just plain comparisons to bin edges.
Steven Lord
el 14 de Abr. de 2020
So instead of 2.5 being in your edges list, replace it with 2.5+eps(2.5) so the edge is just barely greater than 2.5.
edges = 0:0.5:4;
values = edges;
halfToPad = mod(edges, 1) == 0.5 & mod(ceil(edges), 2) == 1;
edges(halfToPad) = edges(halfToPad) + eps(edges(halfToPad))
h = histogram(values, edges);
Bin [0, 0.5+) is higher because the value 0.5 in values is in that bin rather than in bin [0.5+, 1).
Bin [2, 2.5+) is higher because the value 2.5 in values is in that bin rather than in bin [2.5+, 3).
Bin [3.5, 4] is higher because as the last bin it contains both its edges, so both 3.5 and 4 in values are in that bin.
And if you look two elements of edges are just barely greater than the corresponding elements in values.
edges-values
Categorías
Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
