MATLAB: Storing repeated values from a loop

arraycell arraysfor loopmatrixmatrix array

I am unsure how to store the values, then take the average, from my output St into a large matrix so that I can work with the numbers more easily. I have looked through other responses, and cant seem to find a solution. The code is as follows. Any help is greatly appreciated.

Best Answer

Please, never post your code as an image. It actively discourages people to help, because we can't copy and paste code to fix it. The code below should be something like what you mean.
Pay attention to the m-lint warnings. They alert you to potential bugs and possible places to improve your code. Use functions to keep your workspace clean (or at least consider using clearvars instead), and don't use clc if you're not planning on using disp or fprintf.
mu=0.004;
sigma=0.05;
S1=72;
N=261;
K=5;
dt=sigma/sqrt(N);
M=zeros(N,K);
for k=1:K
St=zeros(N,1);St(1)=S1;
for i=2:N
epsilon=randn;%don't overwrite the internal eps function
St(i)=St(i-1)+St(i-1)*(mu*dt+sigma*epsilon*sqrt(dt));
end
M(:,k)=St;
end
plot(M)
xlim([1 N])
xlabel('days')
ylabel('value')
Related Question