determining bin placement, histograms
2 views (last 30 days)
Show older comments
Good evening
I'm using the method of determining bin placement for a histogram and was wondering if this was correct
N = 5;
edges = [0.4,0.8,1.2,1.6,2];
[N,edges,bin] = histcounts(value,'BinMethod','integers');
value = my array. I was wondering if for each edge that each value in my array will go into each corresponding bin. I wasn't sure how the " N ", or " Bin " part of the code works and need some clarification. To clarify also i'd all values at less than 0.4 to be in a bin and then every number below 0.8 to 0.4 to be in a 2nd bin and so forth
1 Comment
Image Analyst
on 25 Aug 2020
Original question in case he deletes it like other posts:
Good evening
I'm using the method of determining bin placement for a histogram and was wondering if this was correct
N = 5;
edges = [0.4,0.8,1.2,1.6,2];
[N,edges,bin] = histcounts(value,'BinMethod','integers');
value = my array. I was wondering if for each edge that each value in my array will go into each corresponding bin. I wasn't sure how the " N ", or " Bin " part of the code works and need some clarification. To clarify also i'd all values at less than 0.4 to be in a bin and then every number below 0.8 to 0.4 to be in a 2nd bin and so forth
Answers (1)
Steven Lord
on 28 Sep 2016
Since you have a set of desired bin edges, I would pass those into histcounts (or histogram if you want to see the results graphically) as the second input and remove the 'BinMethod' parameter name/value pair.
[N, edgesOut, bin] = histcounts(value, edges);
You should see that edgesOut is the same as edges.
If you want a bin before the first value in edges, add -Inf at the start of your edges vector.
[N, edgesOut, bin] = histcounts(value, [-Inf, edges]);
Then the first bin will contain those elements in value whose value is in the range [-Inf, 0.4). Similarly, if you want a bin after the last edge, add Inf at the end to catch values in the range [2, Inf].
2 Comments
Steven Lord
on 28 Sep 2016
If you specify the edges explicitly, you don't need to specify the number of bins as an input. In this case histcounts and histogram compute the number of bins as one fewer than the number of edges.
If you specify the edges explicitly, you don't need to specify 'BinMethod' as an input either. The 'BinMethod' parameter allows you to specify what algorithm histcounts should use to calculate where the bin edges should be. Since you explicitly told histcounts where the bin edges should be, it shouldn't try to calculate new edges but should use what you told it.
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!