The key bit is, as you said, for loops, Matlab doesn't like it, so vectorization is really a keyword. (Together with the preliminary distribution of space.
I just changed a little for the loop section so that you do not have to reset L again and again, instead we save all L in a larger matrix (I also deleted the length(L) command),
L = zeros(N,I); for k=1:N for i = 1:I-1 L(k,(i + 1) * tau ) = L(k,i*tau) + normrnd(mu, sigma); end X(k,1:I) = L(k,1:I); end
Now you can already see that X(k,1:I) = L(k,1:I); in a cycle is outdated, which also means that we can switch the order of the cycles. This is important because i -steps are recursive (depending on the previous step), which means that we cannot vectorize this loop, we can only vectorize k -loop.
Now your source code took 9.3 seconds on my machine, the new code is still needed at about the same time)
L = zeros(N,I); for i = 1:I-1 for k=1:N L(k,(i + 1) * tau ) = L(k,i*tau) + normrnd(mu, sigma); end end X = L;
But now we can apply the vector, instead of looping all the lines (loop through k ), we can exclude this loop and do all the rows "once."
L = zeros(N,I); for i = 1:I-1 L(:,(i + 1) * tau ) = L(:,i*tau) + normrnd(mu, sigma); %<- this is not yet what you want, see comment below end X = L;
Only 0.045 seconds are required for this code on my machine. Hope you still get the same result, because I have no idea what you are calculating, but I also hope that you can see how you go about vectorizing the code.
PS: I just noticed that now we use the same random number in the last example for the entire column, this is clearly not what you want. Instad, you must create a whole vector of random numbers, for example:
L = zeros(N,I); for i = 1:I-1 L(:,(i + 1) * tau ) = L(:,i*tau) + normrnd(mu, sigma,N,1); end X = L;
PPS: Great question!