How do I use histcounts with overlapping bins?

8 views (last 30 days)
First off, there's only this post I found with some relevant inputs, although the comments suggested overlapping bins may not work with histcounts?
My question is this: Is there a way to create bin egdes by giving the number of bins (which histcounts does) and the percentage overlap between bins to generate a set of overlapping bins which can be used with accumarray later on?
More specifically, I have vectors x, y and z covering a spatial volume. I need to "discretize" this volume and bin the vector V.. (which is when I found the answer on 3D binning). I am looking for a way to extend this by adding overlapping bins.
Is there a way to achieve this? Any help is appreciated. Thanks!
  4 Comments
Prodip Das
Prodip Das on 29 Mar 2019
The main use in this case is to generate a more "filled" data set. This could be achieved by making the bins smaller in principle. But if the data to be binned is somewhat sparse then collecting those points over bigger overlapping bins gives a well-averaged effect. This is my understanding of it, which may not be the best reason out there.
Steven Lord
Steven Lord on 29 Mar 2019
Do you need to visualize the overlapping bins (histogram) or just compute with overlapping bins (histcounts)?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Mar 2019
Discretize three times per dimension, once with the bins exactly where you want them, once with the bins [overlap] earlier, once with the bins [overlap] later. Do the 27 different 3D binnings (each possible combination of early, middle, late), taking lists of indices. Then take the union of all of the indices in corresponding bins.
  6 Comments
Prodip Das
Prodip Das on 29 Mar 2019
Thanks Walter.
This is going to take me a while to completely get my head around as its not immediately clear to me.
I'll post the matrix collating bit as a separate question.
Walter Roberson
Walter Roberson on 29 Mar 2019
I think I might have the union loop wrong, possibly.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 28 Mar 2019
Edited: Matt J on 28 Mar 2019
If you're willing to make some approximations in the interest of speed, this is a method that will do the whole 3D accumarray operation. It uses some FEX contributions that you must download, namely KronProd and ndSparse. Basically, it first histograms the x,y,z data normally into super-thin, non-overlapping bins. Then it basically consolidates those into overlapping bins by separable convolution.
%% simulated data
vmin=0; vmax=10; %integer min and max assumed here
x=rand(1,10000)*(vmax-vmin)+vmin;
y=rand(1,10000)*(vmax-vmin)+vmin;
z=rand(1,10000)*(vmax-vmin)+vmin;
%% binning parameter selections
binShift=0.5; binWidth=1;
%% Set-up computations
lowerEdges=vmin:binShift:vmax-binWidth;
upperEdges=lowerEdges+binWidth;
Nbins=numel(lowerEdges);
delta=vmax-vmin;
N=1000*delta;
L=(lowerEdges.')*N/delta+1;
U=(upperEdges.')*N/delta+1;
T=cumsum(sparse(1:Nbins,L,1,Nbins,N+1)-sparse(1:Nbins,U,1,Nbins,N+1),2);
C=KronProd({T(:,1:N)},[1,1,1]); %separable convolution operator
%% Do computation
tic;
e=linspace(vmin,vmax,N);
I=discretize(x,e).';
J=discretize(y,e).';
K=discretize(z,e).';
H=ndSparse.build([I,J,K],1,[N,N,N]);
A=full(C*H); %The "accumarray" result
toc; %Elapsed time is 1.182683 seconds.
  1 Comment
Prodip Das
Prodip Das on 29 Mar 2019
Thanks for the answer Matt ! I wasn't certain where the approximations lay, and wasn't very well versed with separable convolution. Needed a more quick fix as of now, will revert back to this in the future hopefully to understand better.

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!