index = [10 100 1000 10000 100000]
Let's say I wanted to generate 10, 100,...,100000
(corresponding to index above) exponential random variables with parameter 1/10
. I of course did this with a for
loop and with the command for example
x = exprnd(0.1,100,1)
One can also generate the exponential random variables from a uniform random variable with
x = -10*log(u)
In this method, I first generate the uniform random variables then plugged the vector, u
, into the previous equation. This method is faster in terms of computation time.
How might I speed this process up? Could I do this with vectorization, that is, without for loops?
Try this:
expFun = @(size)exprnd(0.1,size,1);
index = [10 20 30 40];
data = cellfun(expFun,mat2cell(index,ones(size(index,1),1),ones(1,size(index,2))),'UniformOutput',false);