View License

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

» Watch video

Highlights from
2D Histogram Calculation

3.9 | 9 ratings Rate this file 12 Downloads (last 30 days) File Size: 1.75 KB File ID: #9896 Version: 1.1

2D Histogram Calculation


Laszlo Balkay (view profile)


06 Feb 2006 (Updated )

Quick computation of two-dimensional histogram of bivariate data

| Watch this File

File Information

function histmat = hist2(x, y, xedges, yedges)
  Extract 2D histogram data containing the number of events of [x , y] pairs that fall in each bin of the grid defined by xedges and yedges. The edges are vectors with monotonically non-decreasing values.

The code is optimized no loop inside, it can be useful in the case of large dataset.
  events = 1000000;
  x1 = sqrt(0.05)*randn(events,1)-0.5; x2 = sqrt(0.05)*randn(events,1)+0.5;
  y1 = sqrt(0.05)*randn(events,1)+0.5; y2 = sqrt(0.05)*randn(events,1)-0.5;
  x= [x1;x2]; y = [y1;y2];
 For linearly spaced edges:
  xedges = linspace(-1,1,64); yedges = linspace(-1,1,64);
  histmat = hist2(x, y, xedges, yedges);
  figure; pcolor(xedges,yedges,histmat'); colorbar ; axis square tight;
 For nonlinearly spaced edges:
  xedges_ = logspace(0,log10(3),64)-2; yedges_ = linspace(-1,1,64);
  histmat_ = hist2(x, y, xedges_, yedges_);
  figure; pcolor(xedges_,yedges_,histmat_'); colorbar ; axis square tight;


This file inspired Hist2 For The People, Histograms For Nd Data, N Dimensional And Circular Histogram Counts, and 3d Histogram.

MATLAB release MATLAB 6.5.1 (R13SP1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
21 Feb 2013 Simone

Simone (view profile)

Doesn't work. When I put in data with a constant value and plot it against something else, it spits out garbage (a diagonal pattern with no sense)

23 Nov 2012 Gunnar Läthén

Regarding John Baldauf's comment:
It's easy to recreate the problem using the first example in the hist2 code. Take e.g.

xedges = linspace(-1,1,64); yedges = linspace(-1,1,64);

and compare with the output of

xedges = linspace(-1,1,62); yedges = linspace(-1,1,64);


xedges = linspace(-1,1,64); yedges = linspace(-1,1,62);

Comment only
16 Feb 2012 Laszlo Balkay

Laszlo Balkay (view profile)

Comment to John Baldauf:
Hi John. Could you point me to your concrete problem, please? If you had data or other doc describing your problem, you can send it directly to my email address:

Comment only
16 Feb 2012 John Baldauf

I'm curious how to get this function to work for me. If I set it up with more yedges than xedges it works great. If I do the opposite it spits out patterns of garbage, sometimes with diagonal stripes or random spots. I'm a very much part-time user and not a hard core guy. Reading thru histc in matlab help and using it left me completely stumped as to what it outputs for what it calls xbins. Any help correcting this is greatly appreciated.

Comment only
06 Feb 2012 Alwan

Alwan (view profile)

Hi. How can I get the actual data points instead of its index. I actually want to add the magnitudes of the data that fall in each bin. Thank you

08 Sep 2011 Laszlo Balkay

Laszlo Balkay (view profile)

Comment to John: You should not transpose the histmat matrix:
The pcolor(xedges,yedges,histmat) will work.

Comment only
01 Sep 2011 John

John (view profile)

I'm having trouble with this routine. In the example below, the x and y ranges dont' seem to be correct, and also the distribution doesn't look right either.

function hist3_test
close all; clc;
events = 1000000;
x=[]; y=[];
for xx=0:.1:1
if (xx<.3)
ys=cos(xs)+ sqrt(0.001)*randn(round(events/nxs),1);
ys=cos(xs)+ sqrt(0.1)*randn(round(events/nxs),1);
x = [x; xs];
y = [y; ys];

% For linearly spaced edges:
xedges = linspace(-1,1,64); yedges = linspace(-1,1,64);
histmat = hist2(x, y, xedges, yedges);
% pcolor(xedges,yedges,histmat'); colorbar ; axis square tight ;

izero = histmat==0;
histmat(izero) = nan;
h=pcolor(xedges,yedges,histmat'); colorbar ; axis square tight ;

Comment only
01 Sep 2011 John

John (view profile)

A surface can be created using


25 Jun 2011 Zbigniew

Matlab's hist3 calculates 2Dhistograms.

Comment only
23 Dec 2010 Radford Juang

Pretty useful. There's a slight bug though with how the final histmat is created.
1. It is transposed when xnbin >= ynbin.
2. It is incorrectly indexed when ynbin > xnbin (because there is a difference in the index being row major and column major)

13 Feb 2010 Nathan

Nathan (view profile)

11 Dec 2006 Narupon Chattrapiban

This code works fine for me
I like the way "histc" is use. Ingenious!

The out of range values should be dumped into a bin other than the bin that contains zeros.

The code might be rewritten as follow:

[xn, xbin] = histc(x,xedges);
[yn, ybin] = histc(y,yedges);

clear xn yn;

%xbin, ybin zero for out of range values
% (see the help of histc) force this event to
% inf
xbin(find(xbin == 0)) = inf; % -> [changed]
ybin(find(ybin == 0)) = inf; % -> [changed]

xnbin = length(xedges);
ynbin = length(yedges);

if xnbin >= ynbin
xy = ybin*(xnbin) + xbin;
indexshift = xnbin;
xy = xbin*(ynbin) + ybin;
indexshift = ynbin;

xyuni = unique(xy);
xyuni(end) = []; % -> [added]
hstres = histc(xy,xyuni);
clear xy;

histmat = zeros(ynbin,xnbin); % -> [changed]
histmat(xyuni-indexshift) = hstres;

20 Sep 2006 Nedialko Krouchev

The 'end-spiel'(main number processing, after dealing with zero bin indexes) part is overcomplicated and can be replaced by an one liner. Hardly as fast as claimed.
Please see the updated entry.

20 Sep 2006 Nedialko Krouchev

The fix to the 'out of range values' is not good as it confounds the two tails of the distribution. Fixed it in my copy.

20 Sep 2006 Nedialko Krouchev

It's odd in itself that Matlab or stats don't seem to provide this function.
Laszlo has made good use of histc toward an even more useful purpose.

20 Mar 2006

Fixing the error due to the out of range values

05 Oct 2011 1.1

I fixed the bugs reported previously.

Contact us