How to compute area under histogram?

20 views (last 30 days)
Dear all, I wanted to compute the area under histogram for certain percent of area. I have a code of creating histogram plot is like this:
hisdataplot = subplot('position', [0.62 0.10 0.15 0.70]);
mindata = min(data);
maxdata = max(data);
nbins = 10;
binrange = linspace(mindata, maxdata, nbins);
ll = histc(data,binrange);
ll2 = zeros(2*length(hist(data)),2);
for kkr=1:length(ll)
ll2(2*kkr-1,:) = [kkr-1 ll(kkr)/360];
ll2(2*kkr,:) = [kkr ll(kkr)/360];
end
ll3 = [0 0; ll2; kkr 0];
plot(ll3(:,1),ll3(:,2)/max(ll3(:,2)),'-k', 'LineWidth',1.0);
set(gca,'Xtick',[0 length(ll)]);
set(gca,'Xlim',[0 length(ll)]);
set(gca,'Ytick',[0 1.2]);
set(gca,'TickLength',[0 0]);
set(gca,'Ylim',[-0.05 1.2]);
Say I want to compute the 80 percent of area and know where it occurs along x-axis. Also, I want to plot this line in the histogram.Is there any command? Kindly let me know. Thanks in advance.
Mahesh
  1 Comment
Siddharth Sundar
Siddharth Sundar on 13 Oct 2014
There is no command that does this directly. Is there a reason that you are computing the area under the histogram? The histogram area varies with bin size, bin width etc. so if you are looking to extract some information, you might want to keep this in mind.
Given the x and y data you give to the plot command (ll3*(:,1) and ll3(:,2)/max(ll3(:,2))), you can compute the area under the whole curve using the trapz command.
To find the exact X coordinate that encompasses a certain area, you need to find the cumulative sum and iterate over it to match the desired percentage area.

Sign in to comment.

Accepted Answer

Mohammad Abouali
Mohammad Abouali on 13 Oct 2014
You can do that and manually compute the 80 percentile value or you can use the matlab function
Y = prctile(X,p)
where X is your data and p is the percentile that you want, so 80.

More Answers (1)

Image Analyst
Image Analyst on 13 Oct 2014
Not sure what toolbox prctile() is in, but I don't have it. You can use this alternative:
cdf = cumsum(ll); % Sum histogram counts to get cumulative distribution function.
cdf = cdf / cdf(end); % Normalize.
% Get data value where 80% is.
dataGT80 = find(cdf>= 0.8, 1, 'first');

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!