MATLAB: I’m trying to set the first for loop (M) to run for the values I have but I can’t figure out how to get it run for the following values and not just the first.

for loopMATLABrandom walkrepeat loop for different values

%% Initial Conditions
N = 2500; % How many total steps per walk
M = [ 100, 1000, 2500, 10000, 25000, 100000, 1000000]; % How many Random Walks are being simulated(Helpful nest for next code)
x_t(1) = 0 ; % Starting Point for X-position
y_t(1) = 0 ; % Starting Point for Y-position
%% Loops and Calculations
for m = 1:M % Number of walks being calculated for in loop
for n = 1:N % Looping all values of N into x_t(n).
A = sign(randn) ; % Generates either +1 or -1 depending on the SIGN of RAND.

x_t(n+1) = x_t(n) + A ; % Based on Factorial process to calculate next x-position
A = sign(randn); % Generates either +1 or -1 depending on the SIGN of RAND.
y_t(n+1) = y_t(n) + A ; % Based on Factorial process to calculate next y-position
end
plot(x_t, y_t) ; % Plots over time as the PERSON is WALKING
hold on
end

Best Answer

Eduardo - try the following
for m = M % Number of walks being calculated for in loop
figure;
for j = 1:m
for n = 1:N % Looping all values of N into x_t(n).
A = sign(randn) ; % Generates either +1 or -1 depending on the SIGN of RAND.

x_t(n+1) = x_t(n) + A ; % Based on Factorial process to calculate next x-position
A = sign(randn); % Generates either +1 or -1 depending on the SIGN of RAND.
y_t(n+1) = y_t(n) + A ; % Based on Factorial process to calculate next y-position
end
plot(x_t, y_t) ; % Plots over time as the PERSON is WALKING
hold on
end
end
When we loop as for m = M, m will be one of each of the elements in the array M. So 100, 1000, etc. Presumably you want all walks for that m to be on the same figure, so we create that figure before generating the m*N walks. Is this what you had in mind when you posed your question?