2D histogram - 'exact' and 'fast' binning, 'crop' and 'stretch' grid adjustment

2D histogram of data pairs [x,y]; 'fast'/'exact' binning; 'stretch'/'crop' grid mode.
Updated 1 Sep 2011

View License

Calculate the 2D histogram of data pairs [x,y] on a 2D grid defined by its edges. Allows 'fast' and 'exact' binning modes. Adjustable input grid mode. Output of mesh centers for easy plot.

binEdges_minmaxN_xy = [10 100 91; 0 10 21];
[h2d, binC_x, binC_y] = hist2(data2d, binEdges_minmaxN_xy, 'fast');
imagesc(binC_x, binC_y, h2d); % a 90x20 grid

In "exact" mode it will equally split each point that falls on the bin edge between the two (or four) adjacent bins. This manner of binning has to be used when box-counting to establish a fractal dimension. It also alleviates problems with floating point numbers or when dealing with heavily aliased or undersampled data.

"on the bin-edge" actually means in an infinitesimally-small vicinity, the default used is 1e-15

[h2d, binC_x, binC_y] = hist2(data, binEdges_minmaxN_xy, 'exact', 1e-9, 'stretch');
In this mode the passed edge meshes are "stretched" so that all data is binned. The step and the locations of the passed edges are preserved.)

In the image one can see the disposition of the original points as well as the output of the two approaches; the 'exact' one (upper-right corner) is not only intuitively correct, it will also make a difference when calculating entropies and fractal dimensions.

On the lower row are plotted the outputs of the two functions already posted on the exchange, one is fast but it has a bug (sometimes it nicks the output so that size(data,2) > sum(h2d(:)), the other one is really slow; none of them provides for this "point-on-the-edge" feature, so I had to write this present function.

In the bottom right corner is the 'fast' output of this function, that is when eps = 0; it should work fine for data coming from simulations, noisy measurements, etc.; for box-counting one has to use the 'exact' option.

Speed :
on my crappy machine it binned 234376 points on a 125 x 125 grid in 0.3 and 14.3 seconds in 'fast' and 'exact' mode respectively; the other functions (the bugged and the slow) did 4.2 sec and 54 sec respectively.
It is also faster than hist3.m, f.i. for 2.8M points is about 20% faster.

For examples and plot commands see inside; any bugs, ideas of improvement, etc., please write me at tudima at jahoo dot com, j -> y

Cite As

tudor dima (2024). 2D histogram - 'exact' and 'fast' binning, 'crop' and 'stretch' grid adjustment (https://www.mathworks.com/matlabcentral/fileexchange/18386-2d-histogram-exact-and-fast-binning-crop-and-stretch-grid-adjustment), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

tags revised

v.1.1 - speed improvement in 'fast' mode (10x-40x);
'fast' mode now faster than hist3.m

2010.06.14 - v. 1.0.1, better output bin export for easier plot
- BSD license added

minor help comments changes