File Exchange

image thumbnail

2D Histogram Calculation

version 1.1 (1.75 KB) by

Quick computation of two-dimensional histogram of bivariate data

9 Ratings



View License

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;

Comments and Ratings (15)


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)

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);

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:

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.


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

Laszlo Balkay

Laszlo Balkay (view profile)

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



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 ;


John (view profile)

A surface can be created using



Matlab's hist3 calculates 2Dhistograms.

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)


Nathan (view profile)

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;

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.

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.

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.



I fixed the bugs reported previously.

Fixing the error due to the out of range values

MATLAB Release
MATLAB 6.5.1 (R13SP1)

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

» Watch video