Scale a histogram to get area = 1
이전 댓글 표시
Hi, I just want to know how to scale a histogram so that the total area is equals to 1.
답변 (4개)
David Young
2011년 3월 3일
If equally-spaced bins are OK, you can do this:
% some data
d = randn(10000, 1);
%compute histogram
nbins = 50;
[h, centres] = hist(d, nbins);
% normalise to unit area
norm_h = h / (numel(d) * (centres(2)-centres(1)));
plot(centres, norm_h);
댓글 수: 5
Walter Roberson
2011년 3월 3일
I was going to give the histc() equivalent, but realized that it might not be well defined. With histc() the last output bin counts the elements that are exactly equal to the last edge, but the width of that bin is 0 and thus so is the area. If you get a non-zero count there, then you cannot calculate a proper normalization.
Jan
2011년 3월 3일
@Walter: Would your HISTC approach work if the last bin is set to Inf?
Walter Roberson
2011년 3월 3일
Maybe. I'm still waking up so I'll think about that later.
Jan
2011년 3월 3일
@Walter: Good morning.
Walter Roberson
2011년 3월 3일
The calculation I was thinking of last night was:
h = histc(d, edges);
norm_h = h(1:end-1) ./ (h(1:end-1) * diff(edges).');
Note that that is a matrix multiply, not an element-wise multiply.
A quick numeric examination shows that if the final edge were inf then the final diff() result would be inf, thus giving an infinite area to the h(end-1) .* (edges(end) - edges(end-1)) portion of the matrix multiplication, which is Not Good.
I'll have to rethink the normalization.
Jan
2011년 3월 9일
1 개 추천
Ayham Alsaoud
2019년 10월 12일
h = histogram(data,"Normalization","pdf");
This is exactly the feature of the probability density function of the histogram (It has area of 1 and stil do the scaling that you wanted)
%% To calculate the Area and try it :
h = histogram(data,"Normalization","pdf");
edges = h.BinEdges;
v = h.Values;
a = 0;
for i =1:size(v,2)
a = a + v(i);
end
area = (edges(2)-edges(1))*s;
댓글 수: 2
Alaa Al-jobory
2020년 5월 13일
Thanks this is what i want...
Walter Roberson
2020년 5월 13일
This is good now; sadly back in 2011 when the original question was asked, Normalization was not an option for histogram()
Alaa Al-jobory
2020년 5월 14일
0 개 추천
but i still have problem i am not need to use histogram(data,"Normalization","pdf") i have an equation p(S)=exp(-(S-S_0 )^2/2σ^2 )/√(2πσ^2 ) i try to polt it like histogram(data,"Normalization","pdf") but it give me different result
Thanks in advance
카테고리
도움말 센터 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!