In Matlab using the rand
routine, how should I write the code to generate 500 samples from an exponential distribution, whose pdf is:
(1/mu)*exp(-x/mu); x>=0
In Matlab using the rand
routine, how should I write the code to generate 500 samples from an exponential distribution, whose pdf is:
(1/mu)*exp(-x/mu); x>=0
If you have the Statistic toolbox you can simply use exprnd
much like you use rand
:
r = exprnd(mu);
where the size of r
will be the size of the mean, mu
, or
r = exprnd(mu,m,n);
where mu
is a scalar mean, and m
and n
are the size of your desired output. If you type edit exprnd
, you'll see that the code is virtually identical to that kindly provided by @LuisMendo. You might find the other functions related to the exponential distribution helpful to, such as exppdf
and expcdf
. These are simple as well and implement basic equation that you can find in your textbook or on Wikipedia.
Assuming you really have to do it using the
rand
function: exploit the property that the minus logarithm of a normalized uniform RV is a normalized exponential RV: