## Histogram gives different BinCounts while appending BinEdges

### B Yin (view profile)

on 29 Apr 2018
Latest activity Answered by Philip Borghesani

### Philip Borghesani (view profile)

on 2 May 2018
Dear all,
I try to shrink the BinEdges in the histogram plot while keeping the remaining BinEdges the same as before. But histogram gives different BinCounts. This should not happen.
Best,
Binglun

Huina Mao

### Huina Mao (view profile)

on 29 Apr 2018
If you use the old function ‘hist’, the same results come. The new function histogram might have some uncertainty. Need MathWorks double check.

### Products ### Star Strider (view profile)

on 29 Apr 2018

You have different numbers of bins in each subplot. In subplot(3,1,1), you define 61 bins, in subplot(3,1,2), 41, and in subplot(3,1,3), 31. Different numbers of bins are going to produce different bin counts.
You can determine this easily enough by calculating them and then looking at the lengths of each vector:
E1 = (13.83: 0.0005 :13.86);
E2 = (13.83: 0.0005 :13.85);
E3 = (13.83: 0.0005 :13.845);

B Yin

### B Yin (view profile)

on 30 Apr 2018
Thanks for your comment. As you can see, the bin edges 13.83: 0.0005 :13.845 are included in all the three plots. The bin counts should be the same in this region since these bin edges are the same.
Actually, the first two plots give the same bin counts. Only the third one is different. Why?
Best,
Binglun
Star Strider

### Star Strider (view profile)

on 30 Apr 2018
Because of the way the colon (link) operator/function calculates the intervals, the values are not exactly the same:
E1 = (13.83: 0.0005 :13.86);
E1a = (13.83: 0.0005 :13.86);
E2 = (13.83: 0.0005 :13.85);
E3 = (13.83: 0.0005 :13.845);
L1 = ismember(E1,E1a);
M1 = nnz(L1);
L2 = ismember(E1,E2);
M2 = nnz(L2);
L3 = ismember(E1,E3);
M3 = nnz(L3);
L4 = ismember(E2,E3);
M4 = nnz(L4);
For example, ‘E3’ has a length of 31 elements, although only 24 of them are the same as those in ‘E2’.
B Yin

### B Yin (view profile)

on 2 May 2018
The use of
>> e1 = 13.83 + (0:60)*0.0005;
>> e2 = 13.83 + (0:40)*0.0005;
>> e3 = 13.83 + (0:30)*0.0005;
seems works.
Then why for the same interval, hist and hitogram give different plots? Which one gives the correct bin counts? See attachments.
Best,
Binglun

### Philip Borghesani (view profile)

on 2 May 2018

The differences in histograms are due to slightly different algorithms used in the two functions. Hist uses the input points as centers for the bins and histogram uses them as edges. This is documented in each functions documentation.
Your code is forcing the histogram routine to compare floating point values at logical equality this will eventually burn you in every situation.
A much better way to produce a histogram for your data is to offset the input bins by 1/2 your minimum quantization value.
h3=histogram( x, (13.83: 0.0005 :13.845) +0.00005);
In addition your instrument appears to have some bias toward certain input values examine:
h3=histogram( x, (13.835: 0.0001 :13.845) +0.00005);
This is magnifying the effect of the comparison errors in the output histograms.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7738217,"math_prob":0.9457127,"size":615,"snap":"2019-43-2019-47","text_gpt3_token_len":191,"char_repetition_ratio":0.12438625,"word_repetition_ratio":0.0,"special_character_ratio":0.35772356,"punctuation_ratio":0.25,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9823262,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-13T03:15:58Z\",\"WARC-Record-ID\":\"<urn:uuid:33622d9a-c9a7-4a1e-949e-b39c2b52b91d>\",\"Content-Length\":\"124248\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a46ff191-88c6-41c2-a271-65682391faf2>\",\"WARC-Concurrent-To\":\"<urn:uuid:9b6a991f-eac6-414d-a390-c4d196fda0ec>\",\"WARC-IP-Address\":\"23.0.20.241\",\"WARC-Target-URI\":\"https://www.mathworks.com/matlabcentral/answers/398027-histogram-gives-different-bincounts-while-appending-binedges\",\"WARC-Payload-Digest\":\"sha1:BKFKE2ATM6RENOSXFRX4KSN4SAUKHCIY\",\"WARC-Block-Digest\":\"sha1:65UGEBE7SF3JK7EGRUNHEUSOZLT24QV4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496665976.26_warc_CC-MAIN-20191113012959-20191113040959-00234.warc.gz\"}"}