How to compute area under histogram?

17 次查看(过去 30 天)
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 个评论
Siddharth Sundar
Siddharth Sundar 2014-10-13
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.

请先登录,再进行评论。

采纳的回答

Mohammad Abouali
Mohammad Abouali 2014-10-13
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.

更多回答(1 个)

Image Analyst
Image Analyst 2014-10-13
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');

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by