Reputation: 101
I am trying to create function that takes a histogram and makes a CDF from it.
However I cannot use the cdfplot
function in Matlab.
How would I go about doing this?
This produces the input histogram:
x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins)
Upvotes: 4
Views: 13012
Reputation: 5822
Use MATLAB's cumsum function, after normalizing the histogram.
hNormalized = h.Values / sum(h.Values);
cdf = cumsum(hNormalized)
The full code:
x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins);
hNormalized = h.Values / sum(h.Values);
cdf = cumsum(hNormalized);
Results with smaller nBins (nBins = 8):
hNormalized =
0.0210 0.0770 0.1930 0.2830 0.2580 0.1250 0.0370 0.0060
cdf =
0.0210 0.0980 0.2910 0.5740 0.8320 0.9570 0.9940 1.0000
Upvotes: 2
Reputation: 18484
The most straightforward way to create a cumulative distribution from data is to generate an empirical CDF. The ecdf
can do this directly. By default, this doesn't require one to produce a histogram for a dataset:
x = randn(1000,1);
ecdf(x);
However, if you want a lower resolution CDF, you can use histogram
directly with the 'cdf'
normalization option:
x = randn(1000,1);
nbins = 25;
histogram(x,nbins,'Normalization','cdf');
You might find the 'cumcount'
option useful too. See the documentation for ecdf
and histogram
for details on how to extract and use the output from these functions.
Upvotes: 3