No BSD License  

Highlights from
2D Histogram Matrix

4.43478

4.4 | 23 ratings Rate this file 89 Downloads (last 30 days) File Size: 1.21 KB File ID: #1487

2D Histogram Matrix

by Kangwon Lee

 

13 Mar 2002 (Updated 18 Jul 2006)

Returns a matrix of number of points in each bins defined by two vectors.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

function mHist = hist2d ([vY, vX], vYEdge, vXEdge)
2 Dimensional Histogram
Counts number of points in the bins defined by vYEdge, vXEdge.
size(vX) == size(vY) == [n,1]
size(mHist) == [length(vYEdge) -1, length(vXEdge) -1]
 
EXAMPLE

mYX = rand(100,2);
vXEdge = linspace(0,1,10);
vYEdge = linspace(0,1,20);
mHist2d = hist2d(mYX,vYEdge,vXEdge);
 
nXBins = length(vXEdge);
nYBins = length(vYEdge);
vXLabel = 0.5*(vXEdge(1:(nXBins-1))+vXEdge(2:nXBins));
vYLabel = 0.5*(vYEdge(1:(nYBins-1))+vYEdge(2:nYBins));
pcolor(vXLabel, vYLabel,mHist2d); colorbar

Acknowledgements
This submission has inspired the following:
2-Dimensional Histogram
MATLAB release MATLAB 5.3.1 (R11.1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (25)
17 Mar 2003 Mark Ward

It does what you'd expect :-)
A surface plot instead of contours would be a useful option. I know - I should add this myself!

17 Feb 2004 Tony Kim

This is what I wanted. Why MATLAB doesn't have this kind of command, I don't understand.

06 Apr 2004 R Corbett

Exactly what I needed

11 Jul 2004 H Chen

Nicely done!

01 Dec 2004 luiz Loures

practical

09 Dec 2004 Kevin Aptowicz

Great!

17 Dec 2004 guiot etienne

simple and fast

24 Feb 2005 Jim Jazz

Useful

25 Feb 2005 Ali H.

Does exactly what it says on the tin! Easy to use, fast, simple, great.

03 Mar 2005 Dimitri Shvorob

Thank you!

07 Mar 2005 Martin Müller

Explicit specification of edges is inconvinient. Why do the vectors have to be passed as a matrix? Plotting of the result often doesnt work. Plot is not a 2d bar chart as expected.

26 Apr 2005 Andrew Meijers

Excellent, it did exactly what it said it would. Great for contouring dense scatter fields.

15 Jun 2005 Jos Groot

Just what I needed.

15 Jun 2005 Nikolaus Correll

IMO one needs to swap lines 27 and 28 if the bins in x and y are not equally spaced. If this is not the case, the version as is gives rotated histograms.

04 Aug 2005 Evan Variano

Works well. It's Short and sweet

08 Dec 2005 Denny Jackson

Workes exactly like I wanted.
Very user friendly.

29 Dec 2005 Iain Strachan

If the histogram spacing is regular and assumed to be divided between the minimum and maximum values, then one can use the useful property of the sparse(i,j,vals) command, that it sums the values assigned to repeated indices, to do a 2d histogram very quickly, something like this...

function m = mk2dhist(vals,n)
% function m=mk2dhist(vals,n)
% make a 2 d histogram nxn
vX = vals(:,1);
vY = vals(:,2);
vX = round((n-1)*(vX - min(vX))/(max(vX)-min(vX)))+1;
vY = round((n-1)*(vY - min(vY))/(max(vY)-min(vY)))+1;
m = full(sparse(vX,vY,ones(length(vX),1)));

I can't yet think of a simple way to exploit this for non-evenly spaced histograms though

05 Jun 2006 Daniel Mark

Hi Kangwon Lee and Iain Strachan
Thank you all:)

10 Jan 2007 Christof Konig Beatty

Lines 12 and 13 (where nEdgeX and nEdgeY are defined) are actually useless and can be omitted.

19 Jan 2008 tudor t

the function has a bug that I was unable to locate, it was faster to write my own function; it affects the last row and the first column, (or viceversa)

for instance with this data
x = [
0 0 1 1 0 0.4 0.6 0.6 0 0.4 0.4 0.4 0.6 0.6 1 1];
y = [
0 1 0 1 0.6 0.4 0 0.6 0.4 0 0.6 1 0.4 1 0.4 0.6];

but although size(x,2) = size(y,2) = 16
when checking sum(sum(bin_2D)) one gets 9 !

14 Mar 2008 Leiguang Wang

very fast. but has a little bug.
in line 38
expression: if (~isempty(vColFound))

this is always true, since even (vColFound == 0)
doesn't mean vColFound is empty .
i think
if (abs(vColFound(:))~= 0) is preferable.

14 Jul 2008 Woot Master

Leiguang Wang: You've misunderstood this code. At line 38 vColFound will be an empty matrix if line 36 doesn't find any values within the given x bin. isempty is the correct function to call here. Your preferred code would break if the y bin spanned 0 and only had counts exactly at 0 in that bin.

I do have a small issue though. The handling of values exactly at the bin edges is different in x and y. Line 36 reads:

vColFound = vCol((vRow > rRowLB) & (vRow <= rRowUB));

To match what histc does in y this should really have >= and < rather than > and <=. This is most likely not a problem in the majority of cases.

24 Nov 2009 Pearl  
10 Feb 2010 Michael Jansz

Very useful but a little buggy.

Depending on you bin sizes you use, the program can crash due to machine rounding errors.

Change lines 25 & 26 in Plot2dHist to read:
vLabelX = linspace(rMinX+0.5*rDeltaX, rMaxX-0.5*rDeltaX, size(mHist2D,2));
vLabelY = linspace(rMinY+0.5*rDeltaY, rMaxY-0.5*rDeltaY, size(mHist2D,1));

fixes this.

06 Aug 2010 Aaron Swan

MATLAB provides the function hist3 to generate bivariate (2D) histograms

Please login to add a comment or rating.
Updates
16 Mar 2002

Adding a separate plotting function

18 Jul 2006

Code cleaning

Tag Activity for this File
Tag Applied By Date/Time
statistics Kangwon Lee 22 Oct 2008 06:42:56
probability Kangwon Lee 22 Oct 2008 06:42:56
histogram Kangwon Lee 22 Oct 2008 06:42:56
matrix Kangwon Lee 22 Oct 2008 06:42:56
points Kangwon Lee 22 Oct 2008 06:42:56
histogram sinara Vijayan 02 Feb 2010 05:27:51
histogram Fatih 12 Oct 2010 08:08:44
potw Lindsay Coutinho 08 Mar 2011 11:13:18
pick of the week Lindsay Coutinho 08 Mar 2011 11:13:18
histogram Andreas 03 Feb 2012 03:13:00

Contact us at files@mathworks.com