필터 지우기
필터 지우기

Plot occurrences of values with bin edges, by using "histcounts" and "plot" functions

조회 수: 16 (최근 30일)
Hi, I have 1000 normally distributed random numbers (with mean = 3 and standard deviation = 10):
X = normrnd(3,10,[1,1000])
I want to partition the X values into 100 bins, and return the count in each bin (i.e. the occurrence of values within each bin), as well as the bin edges:
[n,bin_edges] = histcounts(X,100);
Then, I want to plot the count in each bin together with the values of the bin edges.... Should I (maybe) use the average value of two edges, i.e. the, or is there any other way to plot the count of my values with the bin edges?
plot(bin_edges(1:100),n); % this is not correct, because I plot the left edge for each occurrence of values

채택된 답변

Steven Lord
Steven Lord 2022년 3월 8일
Why not just use histogram?
X = normrnd(3,10,[1,1000]);
h = histogram(X, 100);
c = h.BinCounts;
e = h.BinEdges;
for whichBin = 1:5
fprintf("In bin %d [%g, %g) there were %d elements.\n", ...
whichBin, e(whichBin:whichBin+1), c(whichBin));
end
In bin 1 [-26, -25.417) there were 1 elements. In bin 2 [-25.417, -24.834) there were 2 elements. In bin 3 [-24.834, -24.251) there were 1 elements. In bin 4 [-24.251, -23.668) there were 0 elements. In bin 5 [-23.668, -23.085) there were 2 elements.
  댓글 수: 1
Sim
Sim 2022년 3월 8일
편집: Sim 2022년 3월 8일
Thanks a lot @Steven Lord! Yes, right, the histogram function plots the count of values in each bin and we can extract both BinCounts and BinEdges, but, still, I would like to use the plot function to show markers and lines for the "skyline" of the histogram....... In general, I would like to avoid the typical histogram representation....... :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by