How to divide a column in equal density bins.
Show older comments
I have a column of values of which some are missing (nan). I want to implement a function that discretizes them into 10 equal density bins (not equal width). So, each bin will have approximately the same number of samples and that function returns me original index of all values in each bin. Note: Nan must be ignored. I tried quantile but the values in each bin are different. Any help?
Accepted Answer
More Answers (1)
Image Analyst
on 27 Sep 2015
Perhaps compute CDF and scan along putting 10% of the total into each new, variable-width bin:
% Make random data of "density" so I assume it's a histogram.
myHistogram = randi(20, 1, 1234);
% Randomly make some of them nan's
% Not sure how this would happen with a histogram, but whatever....
nanLocations = randi(length(myHistogram), 1, 33);
myHistogram(nanLocations) = NaN
% Now we can start
% First make NaNs zero.
myHistogram(isnan(myHistogram)) = 0
% Now compute CDF
myCDF = cumsum(myHistogram);
myCDF = myCDF / myCDF(end);
% plot(myCDF);
% grid on;
% Find out how many bins to sum together
% so that we get 10 new bins.
binsToUse = round(length(myHistogram)/10);
% Rebin into 10 bins
edges(1) = 1; % Location of first bin.
for b = 1 : 9
% Find out bin that will give CDFs of 10%, 20%, 30%,...100%
endingBin = find(myCDF < b*0.1, 1, 'last')
edges(b+1) = endingBin;
% Sum those bins to form new histogram
newHist(b) = sum(myHistogram(edges(b):edges(b+1)));
end
% Finish up with last bin.
newHist(10) = sum(myHistogram(edges(9) + 1:end));
% Print to command line
edges
newHist
Categories
Find more on Spline Postprocessing 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!