Issue with moving from hist() to histogram(). Different values?

3 ビュー (過去 30 日間)
Andrew Feenan
Andrew Feenan 2022 年 9 月 30 日
コメント済み: Steven Lord 2022 年 9 月 30 日
Hi,
I am changing this code
[bincount,binpos] = hist(data,min(50,numel(data)/5));
to this, as MATLAB recomends using histogram.
h = histogram(data,min(50,numel(data)/5));
bincount2 = h.BinCounts;
edges = h.BinEdges;
width = h.BinWidth;
binpos2 = edges(1:end-1) + width/2;
The output is
Which is fine. However, I have noticed that from values 1 to 25 bincount and bincount2 have different values. The values are the same from 26 to 50.
It is the exact same issue with binpos and binpos2.
Is there any reason or solution for this?
Andrew

回答 (1 件)

Steven Lord
Steven Lord 2022 年 9 月 30 日
As stated on this documentation page the hist function operates with bin centers while histogram operates with bin edges. If you compare binpos and edges they're likely not the same. See the last section on the page for a discussion of how to convert bin centers to bin edges.
  2 件のコメント
Andrew Feenan
Andrew Feenan 2022 年 9 月 30 日
Thanks for your answer but how are are both bincount and bincount2 not the exact same? The data is exactly the same
Steven Lord
Steven Lord 2022 年 9 月 30 日
The data is exactly the same, but are the bins? In the two examples below I bin the same data. In the first case the first bin is [1, 2) and so only those values in x that are equal to 1 fall into the first bin.
x = randi(2, 1, 10)
x = 1×10
1 2 1 2 1 2 1 2 1 2
[counts1, bins1] = histcounts(x, [1 2 3])
counts1 = 1×2
5 5
bins1 = 1×3
1 2 3
In the second case the first bin is slightly larger [1, 2.001) and so all the elements in x (which are either 1 or 2) fall into the first bin.
[counts2, bins1] = histcounts(x, [1 2.001 3])
counts2 = 1×2
10 0
bins1 = 1×3
1.0000 2.0010 3.0000

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by