5.0

5.0 | 9 ratings Rate this file 80 Downloads (last 30 days) File Size: 2.85 KB File ID: #23897

N-dimensional histogram

by Bruno Luong

 

25 Apr 2009 (Updated 26 Aug 2011)

Compute n-dimensional histogram

| Watch this File

File Information
Description

Like histc but for n-dimension.

Unlike the other nd histogram http://www.mathworks.com/matlabcentral/fileexchange/3957 beside counting, this function returns also the location of points in the bins. Coded entirely in Matlab (no mex required). Speed slightly slower (but quite decent).

User can data and/or function for specific need of accumulator on each patch

MATLAB release MATLAB 7.8 (R2009a)
Other requirements Probably work on most "not-too-old" version
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (15)
29 May 2009 bin zhang

Hi, Bruno:

Your coding is really elegant and excellent. ;-)
I had one question on line 66 " sz(d) = length(ed)-1; ". So here, why sz(d) is not instead length(ed)?

Due to this, when I provide, .i.e, 3 edges of size mx1, for a 3d matrix, I would get *count* as a (m-1)^3 rather than m^3 matrix. Am I misunderstanding sth here, or there is some special reason for this?

Thanks,
Bin

31 May 2009 Bruno Luong

To Bin Zhang:

The way HISTCN is design: If user supply m-edges [e1, e2, ..., e_m] then there are m-1 bins:

bin1 : e1 <= X < x2
bin2: e2 <= X < x3
...
bin-{m-1}: e_m-1 <= X < e_m

The Bin #m does not exist, unless if there is point hitting the right border:

X == e_m

You can indeed force the number of output bins to m by changing the line #66. to

sz(d) = length(ed)-1

Thanks,

Bruno

17 Jul 2009 Daniel Golden

Works great! I'm using it to make a 2-dimensional histogram with an additional third dimension of "weights."

24 Nov 2009 Will Huang

is there any way to represent the 3-dimentional histogram?

I have a 3-dimentional matrix with values in them, is there anyway i can change the size of the dot (bubble) to represent the values in the 3 D plot in matlab

Will

25 Nov 2009 Bruno Luong

Will, you better post the plotting problem on the newsgroup. I suggest to take a look at BAR3 function for the moment.

11 Jan 2010 Jac Billington

This is great, doing just what I wanted. Just one question - I'm plotting a 3-d histogram of x y count. However it always plots the x axis as increasing values from origin and the y axis as decreasing. The reverse y axis doesn't work in m-code - only when I open the plot editor (bit tedious).

Could you possibly point out a tip for getting both axis to increase from origin. I've tried playing with the code but I'm a bit of a matlab novice

Many thanks. Jaclyn

11 Jan 2010 Bruno Luong

Jaclyn, to put y-axis increase from bottom to top call

>> set(gca, 'Ydir', 'normal')

This is question is generic and better posted in the newsgroup. Thanks for the comment.

12 Jan 2010 Jac Billington

Thank you!

13 May 2010 Andrey

Great. 100,000 XY float points in a 512x512 bin matrix takes ~40 ms, and I still get the list of bin assignment!

23 Jul 2010 Soravit

I really appreciate it. Great Work!!

31 Jul 2010 Matthias Fripp

About plotting the results:

Jaclyn, if you just want bars plotted for the pairwise densities of two variables (i.e., a plot with x and y axes corresponding to values of your variables, and vertical bars on the x,y grid, corresponding to the counts for each 2D bin), you can get that with hist3().

Will, if you want to plot weights for 3 variables as varying sizes of bubbles on an x,y,z grid, you can do something like this:

% get the histogram
[count edges mid loc] = histcn(vals);

% make a grid for plotting
[X Y Z]=ndgrid(edges{1}, edges{2}, edges{3});
X=X(:); Y=Y(:); Z=Z(:);

% calculate sizes so the most dense cell gets a value of 100
% also convert from volume to "area" (as if drawing a sphere with
% the right volume and cross-sectional area s)
s_scale = 100/(max(count(:))^(2/3));
s = count(:).^(2/3) * s_scale;
% convert any zeros to small numbers for scatter3
s(s==0)=realmin;

% plot the densities
fh=figure();
set(fh, 'Renderer', 'OpenGL'); % faster drawing
scatter3(X, Y, Z, s, 'filled');

17 Mar 2011 Michal Ficek

Excelent work, it really helped me a lot. Runs fast!

12 Nov 2011 Sanya

Thanks!

20 Jan 2012 J

If I do the example usage but for 2-D data and do fewer random data points (say 200), then when I imagesc it and use plot() to plot the 2-D scatter, it seems like something is just a *little* but off. I'm guessing the code is working properly but that the usage example perhaps is plotting incorrectly to match up properly with a scatter plot? It's not a simple axis xy shift or flipup/fliplr on a single dimensions... the plots look similar but almost a reflection about the symmetry axis is required? Maybe I'm just missing something...

06 Feb 2012 mahmut  
Please login to add a comment or rating.
Updates
25 Aug 2011

New feature: accumulate user's data rather than 1

26 Aug 2011

User specific function for accumulator

Tag Activity for this File
Tag Applied By Date/Time
histogram Bruno Luong 27 Apr 2009 11:01:03
hist Bruno Luong 27 Apr 2009 11:01:03
histc Bruno Luong 27 Apr 2009 11:01:03
multi dimensional Bruno Luong 27 Apr 2009 11:01:03
pdf Bruno Luong 27 Apr 2009 11:01:03
probability Bruno Luong 27 Apr 2009 11:01:03
nd Bruno Luong 27 Apr 2009 11:01:03
nd Dmitry 26 Mar 2010 04:08:32
accumarray Bruno Luong 26 Aug 2011 09:22:28

Contact us at files@mathworks.com