How do I get the same bin width for two different data sets using the histfit command?

I have a problem with the bin widths in the histfit command: I want to compare two lognorm-distributed data sets with different sample sizes (A is a 530x1 array, B is a 335x1 array), so I created a histogram with using the hisfit command with normalized data. The code I used:
h1=histfit(A,30,'lognorm')
hold on
h2=histfit(B,24,'lognorm')
%Generating normalized data
N1=sum(h1(1).YData);
N2=sum(h2(1).YData);
h1(1).YData=h1(1).YData/N1;
h1(2).YData=h1(2).YData/N1;
h2(1).YData=h2(1).YData/N2;
h2(2).YData=h2(2).YData/N2;
alpha(h1(1),.5)
alpha(h2(1),.5)
uistack(h1(2),'top')
uistack(h2(2),'top')
xlim([0 3000])
ylim([0 0.2])
hold off
This code gives me the following Histogram:
As you can see, the bin width is not the same for both data sets. Does anybody know a method to generate a histogram with two normalized data sets, the lognorm fits and the same bin widths?

 Risposta accettata

Amin = min(A(:)); Amax = max(A(:));
Abins = 30; %from histfit() call in original code
Abinwidth = (Amax - Amin)/Abins;
Bmin = min(B(:)); Bmax = max(B(:));
Bbinwidth = Abinwidth;
Bbins = (Bmax - Bmin)/Bbinwidth;
Except... Bbins from this calculation is likely not to be an integer. To get the same bin width for each of them, you would need to adjust the number of bins like
[N, D] = numden((Bmax - Bmin)/(Amax - Amin))
and if those values exceed the number of samples then you simply cannot create equal sized bins, and you would have to start considering what ratio would give you "close enough" to equal sizes.

1 Commento

Thank you so much, with numden I obtained the ratio for optimal binwidths which was 8489/10000 which I rounded to 17/20, so 40 bins for A and 34 for B did the trick!

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2021a

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by