MATLAB Language Random Walk


The following is an example that displays 5 one-dimensional random walks of 200 steps:

y = cumsum(rand(200,5) - 0.5);

legend('1', '2', '3', '4', '5')
title('random walks')

In the above code, y is a matrix of 5 columns, each of length 200. Since x is omitted, it defaults to the row numbers of y (equivalent to using x=1:200 as the x-axis). This way the plot function plots multiple y-vectors against the same x-vector, each using a different color automatically.

random walks plot