To generate random numbers following an exponential distribution in MATLAB, you can use the exprnd
function from the Statistics and Machine Learning Toolbox. The syntax for exprnd
is:
main.m21 chars2 lines
where mu
is the mean of the distribution, and m
and n
specify the size of the output array. X
will be an array of size m
-by-n
containing random numbers generated from an exponential distribution with mean mu
.
For example, to generate 1000 random numbers from an exponential distribution with mean 2, you can use the following code:
main.m33 chars3 lines
You can also plot the probability density function (pdf) and cumulative distribution function (cdf) of the exponential distribution using the exppdf
and expcdf
functions, respectively:
main.m244 chars16 lines
This will generate a plot with the pdf and cdf of the exponential distribution with mean 2, ranging from 0 to 10.
gistlibby LogSnag