How Can I fix the width of Bins?

Hi Every body, I would like to tell you that I am beginner in Matlab. So please I need your help.
I want to fix the width of bins of the histogram, My code is:
% cd F:\Script\Center_Marsh\Center_Marsh_1992
H= Dec_1992(:);
hist(H);
subplot(1,2,1)
hist(H)
xlabel('NDVI','fontsize',14)
ylabel('AVHRR Pixels','fontsize',14)
set(gca,'XMinorTick','on')
set(gca,'YMinorTick','on')
set(gca,'XGrid','on')
set(gca,'YGrid','on')
set(gca,'GridLineStyle',':','linewidth',1)
set(gca,'TickDir','out')
set(gca,'TickLength',[.015 .025])
axis([0.0 0.5 0 110])
save('Dec_Hist_1992.fig');
D= Dec_1992(:);
[hi,cx]=hist(D);
subplot(1,2,2), plot(cx, hi,'k', 'LineWidth', 3)
xlabel('NDVI','fontsize',14)
ylabel('AVHRR Pixels','fontsize',14)
set(gca,'XMinorTick','on')
set(gca,'YMinorTick','on')
set(gca,'XGrid','on')
set(gca,'YGrid','on')
set(gca,'GridLineStyle',':','linewidth',1)
set(gca,'TickDir','out')
set(gca,'TickLength',[.015 .025])
axis([0.0 0.5 0 110])
save('Dec_Hist_1992.fig');
Thank you
Reyadh

4 Comments

Walter Roberson
Walter Roberson on 30 May 2015
Edited: Walter Roberson on 30 May 2015
You left a lot unspecified about how the bins are to be arranged. Suppose, for example, the data turned out all to be within the width of one bin: should one bin be used that covers the whole data, or should two bins be used, each covering half of the data? Suppose one bin would "just" cover it: should one bin be used or two? If two bins would "just" cover some data, should two bins be used or three?
With fixed width bins, if N bins is just short of being enough to cover the entire range, then there are ways to arrange to cover the data with either N+1 bins or with N+2 bins. In the case where 0 bins is just short of being enough to cover the range because the range is less than 1 bin-width wide, then you can either use 1 bin to cover the range or you can use 2 bins to cover the range. What rule do you want to decide?
Hi Walter Roberson, I would like to cover the whole data.
Reyadh
I would like to Fix the Number of Bin and its Width with the whole data.
Thank you
Reyadh
If you fix the number of bins and you also fix the bin width, then it might not be possible to fit all of the data. Even in cases where it is possible, you have ambiguity.
Imagine that the requested bin width is 2 and imagine that there is 1 point to be covered. Possible coverings include
|x |
| x |
| x|
The bin width was fixed, the number of bins was fixed, but the policy was not fixed.
Why does it matter? Because of fairness. Consider 2 bins of fixed width, and 4 points.
| xx x| x | -> centered but uneven distribution
| xx |x x | -> not centered but equal distribution
| x|x x x| -> not centered, unequal
There are techniques to dynamically move the boundaries to get the most even distribution (defined by lowest sum of squares of number of elements in each bin), but for a simple histogram you place your boundaries first and then you count once, not knowing ahead of time how the points distribute. So basically you start with the information about where the first point is and where the last point is; and you have forced the number of bins and the bin widths; and now you need a policy about where to put the bin centers to distribute the mod((max-min),bin_width) extra space.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 30 May 2015
I would use hist to calculate the histogram information, then use bar to do the plot, since it will let you vary the width of the bars.

2 Comments

Hi Star Strider, How can I use that with my code. I am beginner in matlab.
Thank you
Reyadh
My pleasure.
I actually thought you intended bar width, since it is easy to set the bins in the hist function or its friends.
To get the histogram data to use with the bar plot, request it as an output from hist. See the documentation for Use hist to Calculate Only. (I see that in your code, but the documentation shows how to use it with bar.)
To set the bar width for the entire bar plot, see the documentation for Specify Bar Width.

Sign in to comment.

Categories

Asked:

on 30 May 2015

Commented:

on 30 May 2015

Community Treasure Hunt

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

Start Hunting!