can anyone tell me an intuitive way of finding number of bins for histcounts function

6 views (last 30 days)
The binning algorithm used by matlab is very expensive [N,edges] = histcounts(I) Instead I am looking for some different and computationally less expensive ways in which we can find the bins [N,edges] = histcounts(I,bins);

Accepted Answer

Steven Lord
Steven Lord on 11 Sep 2018
Edited: Steven Lord on 30 Oct 2018
There are several different possible BinMethod values available for histcounts. The default is 'auto' which attempts to find a "good" choice of bin width. That does take some effort. Consider explicitly specifying one of the other options. The 'sturges' and 'sqrt' BinMethods are probably going to be the least computationally intensive, as instead of performing computations on the whole data set ('scott' calls std and 'fd' computes the IQR) they use numel(x) in their calculations.
Or if you have a sense of how wide the bins you want to use are and where the first and last bins should be located, you can specify the bin edges vector yourself as the second input. But this requires you to have some knowledge of the data on which you're operating.
[SL: fixed a typo, "IRQ" -> "IQR"]
  3 Comments
Steven Lord
Steven Lord on 11 Sep 2018
Call histcounts and specify the 'BinMethod' option. On the documentation page for the histcount function see the "Determine Bin Placement" example. That example uses the 'integers' BinMethod, so to use sturges or sqrt change 'integers' to 'sturges' or 'sqrt'.
Walter Roberson
Walter Roberson on 11 Sep 2018
sturges: look somewhere near line 440 of histcounts to see the sturgesrule function. It calls matlab.internal.math.binpicker
>> which -all matlab.internal.math.binpicker
/Applications/MATLAB_R2018a.app/toolbox/matlab/datafun/+matlab/+internal/+math/binpicker.m % static method or package function
You can edit that.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!