Histogram with overlapping bins
Show older comments
Is there a fast way to code this ?
Say, X= [101 202 303 505] is the set of values to be binned,
and Y=
[0 100 200 300 400; 200 300 400 500 600] has information about the bin-edges, with the first row containing lower-bin edges and the second row containing upper bin-edges (so that successive bins are 0-200, 100-300, 200-400, 300-500, and 400-600)
and the result is [1,2,2,1,2].
Normally I would code this as:
out=NaN(1,size(Y,1)); for i=1:length(out) out(i) = length(find( X<=Y(2,i)&X>Y(1,i) ); end
Is there a faster/more succinct way, using a vectorized function ?
Thanks, Suresh
Accepted Answer
More Answers (1)
Bruno Luong
on 24 Feb 2011
You might try this code using my mcolon function:
% Data
Y=[0 100 200 300 400;
200 300 400 500 600]
X= [101 202 303 505]
% Full vectorized Engine
lo = Y(1,:);
hi = Y(2,:);
nbin = size(lo,2);
[~, ilo] = histc(X, [lo Inf]);
[~, ihi] = histc(X, [-Inf hi]);
% Test if they belong to the bracket
tf = ilo & ihi & (ilo >= ihi);
left = ihi(tf);
right = ilo(tf);
loc = mcolon(left,right); % FEX
count = accumarray(loc(:),1,[nbin 1])'
Bin belonging follows closed-left/open-right bracket convention. Reverse the sign of X, Y if you prefer the opposite.
2 Comments
s k
on 25 Feb 2011
Bruno Luong
on 25 Feb 2011
I never see the same bin-width, or pair-wise overlapping has been specified in the question. It just shows as such in the example.
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!