% Set T and N T=1000; N=10; % Create some pseudo random numbers e=randn(T,N); % Initialize a place for our random walks rw=zeros(T,N); % Initialize the recusions rw(1,:)=e(1,:); % Loop over t for t=2:T % loop over n for n=1:N % The actual calculation rw(t,n)=rw(t-1,n)+e(t,n); end end plot(rw) % Another way that uses one row at a time and avaois the inner loop rw2=zeros(T,N); % Initialize the recusions rw2(1,:)=e(1,:); % Loop over t for t=2:T % The actual calculation rw(t,:)=rw(t-1,:)+e(t,:); % Note the use of : to mean all columns end % Another way using cumsum rw3=cumsum(e); % Make sure they are identical disp([' The max absolute difference is ' num2str(max(max(abs(rw-rw3))))])