How can I create histograms of relative frequency?
25 views (last 30 days)
Show older comments
I want to create a histogram of the relative frequency count over regualar intervals.
The relative frequency is the number of instances of data in a particular range divided by the total number of data points.
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
The following example plots a bar chart of the relative frequency of data "y" in the data range -8 to 94 at intervals of 17.
The data is as follows:
y = [45 40 28 51 18 35 29 21 29 22 38 24 34 37 43 44 48 ...
28 9 41 31 40 24 17 15 37 33 35 28 25 25 31 29 44 ...
41 43 29 25 45 17 61 94 51 39 50 37 40 64 34 20];
Calculating the desired interval width, and creating the desired data range:
numIntervals = 5;
intervalWidth = (max(y) - min(y))/numIntervals;
x = -8:intervalWidth:94;
Next, use the HISTC function to find the frequency of each data range "x" in the given data set "y". This function returns the histogram count for a data set and range.
ncount = histc(y,x);
Calculate the relative frequency of each data range by dividing the frequency by the total number of data points:
relativefreq = ncount/length(y);
Finally plot the relative frequency versus the data ranges as a bar chart. On this chart, the bars will be adjoining, and the tick marks on the x-axis will label the extent of each bar's data range.
bar(x-intervalWidth/2, relativefreq,1)
xlim([min(x) max(x)])
set(gca, 'xtick', x)
Note that the BAR function centers the bars it creates on the supplied x-data. So this data has been translated by half an interval so the extents of the bar width fall on the limits of the data ranges.
0 Comments
More Answers (0)
See Also
Categories
Find more on Bar Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!