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
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:
samples = -mu*log(rand(1,500));
Use random
function.
For example to create a 4*6 matrix with mu=1.3 with an exponential distribution use:
random('Exponential',1.3,4,6)
or
random('exp',1.3,4,6)
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.