Using histogram() data without plotting or calculating the figure.

93 vues (au cours des 30 derniers jours)
Thomas Houllier
Thomas Houllier le 9 Jan 2016
Commenté : Erik Lindskog le 12 Oct 2022
Hi, this is my first post here.
I really need to optimize a function so as to make it as fast as possible. In it, I use the histogram function. All I need is the histogram().Values and histogram().BinEdges data. I don't need to plot anything and I need to keep the computation time to the bare minimum. The code I use is roughly:
fig=figure; set(fig,'visible','off');
H=histogram(...);
h=H.Values;
b=H.BinEdges;
% Some data treatment
%%%%
close(fig);
By doing 'tic-tocs' I found out that these precise lines were taking up 90% of the execution time. Is there any way to have access to the histogram data without opening a figure, or any other way of optimizing time ? I mean not opening a figure at all, not just hiding it, as I've already done that.
Thanks in advance !
  1 commentaire
Erik Lindskog
Erik Lindskog le 12 Oct 2022
Possibly a different way to compute a histogram without plotting a figure is to use the function histcounts to compute the histogram counts and edges. Later the histogram can then be plotted with the histogram function.
E.g.:
[counts, edges] = histcounts(data, 100) % Computes the histogram counts and edges for 'data' without plotting
...
histogram('BinEdges', edges, 'BinCounts', counts) % Plots a histogram with edges 'edges' and counts 'counts'.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 9 Jan 2016
If you only want the histogram values and not the plot, use the histcounts (or histc) functions.
  5 commentaires
Steven Lord
Steven Lord le 4 Mar 2021
The second output from histcounts contains the edges.
Amir Pasha Zamani
Amir Pasha Zamani le 2 Avr 2021
Thanks Star
I have the same problem with my codes efficiency
But even with histcount the code is still slow

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by