Two bar graphs - two data sets - different bin width

Hi everyone,
I am kinda new to matlab and may not have been asking the right question to google. Anyway, here is my problem: I've got two data sets called one and two:
none = length(one)
ntwo = length(two)
%h = nan(max(none,ntwo),2)
h(1:none,1)=one
h(1:ntwo,2)=two
From there I extract the best bin width and the theoretical best amount of bin for each data set ( following the The Freedman-Diaconis rule):
%one -------
minNumberOne = round(min(h(1:none,1)))
maxNumberOne = ceil(max(h(1:none,1)))
iqrVarOne = iqr(h(1:none,1))
hOne = ceil(2 * iqrVarOne * none^(-1/3))
binsOne = ceil((maxNumberOne - minNumberOne)/hOne)
%two -------
minNumberTwo = round(min(h(1:ntwo,2)))
maxNumberTwo = ceil(max(h(1:ntwo,2)))
iqrVarTwo = iqr(h(1:ntwo,2))
hTwo = ceil(2 * iqrVarTwo * ntwo^(-1/3))
binsTwo = ceil((maxNumberTwo - minNumberTwo)/hTwo)
From now I am a bit lost. I'd like to apply for each bar graph the number of bin and the bin width, AND displaying those graph on the same figure, like that:
My code:
figure(1)
data = [h(:,1) h(:,2)]
[y, x] = hist(data)
bar(x,y, 'group')
this code doesn't take into account the bins calculated before of course. Do you have any ideas on how to integrate that to my code?
Cheers everyone. I hope I have been clear enough...
Flo

Antworten (2)

José-Luis
José-Luis am 4 Jul. 2016

0 Stimmen

one = rand(100,1);
two = rand(100,1);
nOne = 10;
nTwo = 15;
figure
histogram(one,nOne);
hold on;
histogram(two,nTwo);
%clearer
figure
ksdensity(one);
hold on;
ksdensity(two)

9 Kommentare

Flo
Flo am 4 Jul. 2016
That actually works, but instead of having those kind of graph I'd like to control the bin sizes independently. would you know on how to do that ?
I keep your solution for now :)
thx
José-Luis
José-Luis am 4 Jul. 2016
Please read the documetation for histogram. Sounds like you might want to define custom edges.
Flo
Flo am 4 Jul. 2016
I'll have a look on that (already did to be honest). My main concern was to know whether it was feasible or not to keep the 'grouped' option with my kind of problem.
José-Luis
José-Luis am 5 Jul. 2016
I'm not sure I follow. How do you want your plot to look?
Flo
Flo am 5 Jul. 2016
I'd like my plot to follow the presentation of the one I posted in the first place (grouped) if such a thing is possible.
José-Luis
José-Luis am 5 Jul. 2016
How would the grouping work if your custom edges have different sizes?
Flo
Flo am 5 Jul. 2016
That was my very first question :)
José-Luis
José-Luis am 5 Jul. 2016
Such a plot might be a mess depending on how you bins intersect though...
By looking on google I've found a way apparently. It may interest some person. Let me know what you think about it:
figure(1);
[dummy, t] = hist([one;two], numBin);
nx = hist(one, t); % Sort x into bins.
nx = transpose(nx/sum(nx));
ny = hist(two, t); % Sort y into bins.
ny = transpose(ny/sum(ny));
bar(t, [nx, ny]);

Melden Sie sich an, um zu kommentieren.

Duncan Po
Duncan Po am 5 Jul. 2016

0 Stimmen

histogram/histcounts have builtin support for Freedman-Diaconis rule:
histogram(x, 'BinMethod', 'fd')
or
[n, binedges] = histcounts(x, 'BinMethod', 'fd')

Gefragt:

Flo
am 4 Jul. 2016

Beantwortet:

am 5 Jul. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by