MATLAB: How to bin 2d data

bin 2d data; 2d histogram; hotmapStatistics and Machine Learning Toolbox

Dear all,
I have a 2-dimension coordinate set (x,y) which contains 5000 points and I want to construct a 2d map to reflect the point density distribution. firstly I want to divide both of the ranges of the two coordinate sets (-3 to2 for x; and -2.5 to 3 for y) into like 37 bins,so there will be totally 37×37 bins. Then I want to assign each point into these bins.To do that I need to construct a indicator function δkij (k=1:5000; i=1:37; j=1:37) which denotes whether point k falls in bin (i,j): if point k falls into bin (i,j), then δkij=1, otherwiseδkij=0. Then the histogram at bin (i,j) is the sum of δkij (k=1:5000). At last I wish to plot a contour map or hotmap to show the hisrogram. How does matlab realize these computation?
All the best!
Yeping Sun

Best Answer

Try (the badly named) hist3() to construct a 2-D histogram:
% Create sample data.
numPoints = 5000;
x = -3 + 5 * rand(numPoints, 1);
y = -2.5 + 5.5 * rand(numPoints, 1);
% Now construct the histogram
xy = [x, y];
subplot(1,2,1);
hist3(xy, [37, 37]); % Plots bar chart.
subplot(1,2,2);
counts = hist3(xy, [37, 37]);
imshow(counts, []); % Show as image
colormap(hot(256));
colorbar;
axis on;