'Cumulative distribution in Matlab
I have a question on plotting probability distribution and cumulative distribution curves using Matlab. I apologize for asking a noob question but I am new to Matlab, having only used it for a few hours.
I have a set of data which has the size range for the sand particles found on a beach in millimeters (e.g: >2.00, 1.00–2.00, 0.50–1.00, <0.50). And their corresponding percentages of finding these sand particles are as follows: (e.g.: 30, 25.5, 35.9, 8.6).
How am I supposed to input the values in the Matlab system for it to plot the probability distribution and cumulative distribution curves on the same plot with different colors? Percentage should be the y-axis and the size range should be the x-axis.
Solution 1:[1]
If your dataset is literally 4 points, then you can simply enter them literally. For example, if my dataset was {(A, 1), (B, 2), (C, 3)}, then we could simply set y = [1, 2, 3] and x = {'a', 'b', 'c'}.
For distributions, you should take a look at the sum and cumsum functions.
For plotting, take a look at bar for frequency plots and plot for cumulative plots (this is just my preference). The documentation contains information on setting colors.
For plotting on the same graph, look at hold. To label your plot and your axes, look at xlabel, ylabel, and title.
Matlab has a good FAQ on setting the actual values that are displayed on each axis. For example, I can plot my dataset above by plotting the y vector only, and then setting the X tick labels to 'A', 'B', and 'C'.
Solution 2:[2]
I'd be careful with the cumulative distribution function (CDF). It might make more sense to reorder your data in increasing particle size (see fliplr() function) otherwise your CDF's interpretation will be suspect.
The cumsum() function can get your CDF from the given probability mass function (PMF).
label={'<0.50','0.50-1.00','1.00-2.00','>2.00'}';
pmf = [0.086 0.359 0.255 0.30]';
cdf = cumsum(pmf);
bar(pmf) % PMF
set(gca,'XTickLabel',label)
title('Sand Particle Size Distribution')
xlabel('Sand particle size (mm)')
figure
stairs(cdf,'ks-','LineWidth',2.0) % CDF
set(gca,'XTick',1:length(label),'XTickLabel',label)
ylabel('Percentile')
xlabel('Sand particle size (mm)')
ylim([0 1])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Max |
| Solution 2 |


