View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

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

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

5.0 | 2 ratings Rate this file 4 Downloads (last 30 days) File Size: 4.77 KB File ID: #18386 Version: 1.6
image thumbnail

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


tudor dima (view profile)


21 Jan 2008 (Updated )

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

| Watch this File

File Information

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

MATLAB release MATLAB 6.5 (R13)
Other requirements none
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
15 Jul 2010 Rhys

Rhys (view profile)

Brilliant mfile! I didn't particularly need the 'exact' mode but the function is very easy to use. You have my thanks!

Comment only
15 Jul 2010 Rhys

Rhys (view profile)

17 May 2008 Jonathan Sperber

Very good, thank you ! I needed something like this for calculating a fractal dimension, it saved me a lot of time. Thanks again !

14 Jun 2010 1.2

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

28 Aug 2011 1.5

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

01 Sep 2011 1.6

tags revised

Contact us