# Computing CDF????

232 views (last 30 days)
Talha Rahman on 19 Dec 2011
Hi all,
Does anyone of you know how to calculate cdf plot using following data sets:
A = [10 10 10 5 3 3 3 2 0]; B = [10 10 10 5 5 3 3 2 2 2 1 0]; how can obtain cdf from A and B?
Best Regards Talha

Steven Lord on 30 May 2018
If you're using release R2014b or later (for histcounts and histogram) or release R2015b or later (for histcounts2 and histogram2) you can specify the 'Normalization' argument with value 'cdf' to display the CDF of the data that you binned using the appropriate function.
Note that this doesn't find parameters for a particular distribution that were determined by fitting the distribution to your data. If you want to do that, take a look at some of the functions on this page in the documentation for Statistics and Machine Learning Toolbox, specifically those whose names end in "fit".
Camilo Malagon Nieto on 31 May 2018
Thank you for the update. Then the solution will be:
Given....
A = [10 10 10 5 3 3 3 2 0]; B = [10 10 10 5 5 3 3 2 2 2 1 0]
First merge the data in a new vector
X=[A,B]
The plot the Cumulative distribution histogram
histogram(X,'Normalization','cdf')

Image Analyst on 19 Dec 2011
Try this code. It's actually just two calls - one to hist() and one to cumsum(), but it's a full-fledged demo with all kinds of fancy plotting and does it for both A and B, so it looks long and complicated but it's actually not. (It only took about 3 minutes to code up this demo). Don't be afraid - just copy, paste, and run:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
A = [10 10 10 5 3 3 3 2 0];
B = [10 10 10 5 5 3 3 2 2 2 1 0];
% Compute the histogram of A and B.
[countsA, binsA] = hist(A);
[countsB, binsB] = hist(B);
% Compute the cumulative distribution function of A and B.
cdfA = cumsum(countsA) / sum(countsA);
cdfB = cumsum(countsB) / sum(countsB);
% Plot the probability distribution of A.
subplot(2,2, 1);
bar(binsA, countsA);
title('Histogram of A', 'FontSize', fontSize);
ylabel('Count A', 'FontSize', fontSize);
xlabel('Values of A', 'FontSize', fontSize);
grid on;
% Plot the probability distribution of B.
subplot(2,2, 2);
bar(binsB, countsB);
title('Histogram of B', 'FontSize', fontSize);
ylabel('Count B', 'FontSize', fontSize);
xlabel('Values of B', 'FontSize', fontSize);
grid on;
% Plot the cumulative distribution function of A.
subplot(2,2, 3);
bar(binsA, cdfA); % You can use plot() if you want to.
title('CDF of A', 'FontSize', fontSize);
grid on;
ylabel('Percentage A (/100)', 'FontSize', fontSize);
xlabel('Values of A', 'FontSize', fontSize);
% Plot the cumulative distribution function of B.
subplot(2,2, 4);
bar(binsB, cdfB); % You can use plot() if you want to.
title('CDF of B', 'FontSize', fontSize);
ylabel('Percentage B (/100)', 'FontSize', fontSize);
xlabel('Values of B', 'FontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

Talha Rahman on 19 Dec 2011
Thanx Image Analyst, that is a great explanation. Let me elaborate what i was asking. I meant, is it possible to have single CDF curve from both A and B. A and B are the outcomes of same function that i ran two times and gave me A and then B. i want to know distribution of the outcomes.
Best Regards
Talha

Ssssssss fffgffggg on 9 Jul 2017
how to plot exp cdf with histogram?????
##### 2 CommentsShowHide 1 older comment
Image Analyst on 9 Jul 2017
This does not answer the original poster's question. To get a CDF and histogram they should call cumsum() and histogram(), not rand(). Anyway, they posted it 6 years ago so it's likely they don't need an answer anymore, despite the fact that they never accepted the answer that was offered.

Camilo Malagon Nieto on 30 May 2018
Edited: Camilo Malagon Nieto on 30 May 2018
There is not a function that automatically takes a data vector and creates a vector with CDF values.
You need first to see your data by plotting it with a histogram
hist(X)
then use the distribution tool to see what distributions may suit your data
disttool
then use the distribution fitting tool to see how that distribution actually fits your data
dfittool
Then create a new distribution Object that fits your data
NewDist=fitdist(X,'Normal');
Than use the new distibution to plot the CDF
cdfplot(NewDist)
if you need values from the CDF you will have to create a values vector and the values to plot and get the CDFs for them
t=0:0,1:10;
Xcdf = cdf(NewDist,t);
Then you can plot the same
plot(t,Xcdf);
Camilo Malagon Nieto on 30 May 2018
Worth to notice that the data provided by the question does not have statistical meaning due to the poor amount of samples.