Index exceeds the number of array elements (1).

s(1) = 100; % Start at 100
step1 = 0:1; % The step goes from 0 to 1
F1 = [25000, 100000]; %The number of simulations for the walker
for Z = 1:length(F1) %Retrieves the length of each simulation
for B = 1:F1(Z)
for L = 100:2601 % This is for the 2500 steps, and we repeat the functions from part 1
o(L) = L; % This keeps track of the time
d(L) = rand; %Retrieves a random value for the walker
if d(L) > .5
s(L) = s(L-1) + step1; %The walker steps to the right
elseif d(L) < .5
s(L) = s(L-1) - step1; %The walker steps to the left
else
s(L) = .5;
end
end
Endp(B) = s(L); %This stores the poisiton for each simulation
end

7 Comments

I can't figure out how to fix the problem, or what it is. Any help would be appreciated. Thanks.
We don't have all the variables so we can't run your code. We also don't know which line is causing the error.
ok here's the full code, i apologize. the error is in line 80 which is: s(L) = s(L-1) + step1; %The walker steps to the right
%% Part 1
d(1) = rand ; % Choosing a random number
a(1) = 0 ; % Intial Position of Walker
o(1) = 1 ; % Intial time of the walk
step = 1 ; % The stepsize of the walker
for i = 2: 2500 % The walker takes 2500 steps
o(i) = i ; % This keeps track of the time
d(i) = rand ; %Retrieves a random value for the walker
if d(i) > .5
a(i) = a(i-1) + step ; %The walker steps to the right
elseif d(i) < .5
a(i) = a(i-1) - step ; %The walker steps to the left
end
end
figure('Name','A Random Walk of 2500 steps')
comet(o, a())
title('A Random Walk of 2500 steps');
xlabel('Progression of Steps');
ylabel('Position of the Walker');
grid on
%% Part 2
p(1) = 0;
F = [1000, 2500, 10000, 25000, 100000, 1000000]; %The number of simulations for the walker
for A = 1:length(F) %Retrieves the length of each simulation
for B = 1:F(A)
for L = 2:2501 % This is for the 2500 steps, and we repeat the functions from part 1
o(L) = L; % This keeps track of the time
d(L) = rand; %Retrieves a random value for the walker
if d(L) > .5
p(L) = p(L-1) + step; %The walker steps to the right
elseif d(L) < .5
p(L) = p(L-1) - step; %The walker steps to the left
else
p(L) = .5;
end
end
Endp(B) = p(L); %This stores the poisiton for each simulation
end
imp = sprintf('%g th Simulation of 2500 Steps', F(A));
figure('Name', imp)
histogram(Endp()) %This plots the end point for each simulation
title(imp)
xlabel('The Position of the Walker')
ylabel('The Number of Time the Walker is in that Position')
grid on
end
%% Part 3
s(1) = 100; % Start at 100
step1 = 0:1; % The step goes from 0 to 1
F1 = [25000, 100000]; %The number of simulations for the walker
for Z = 1:length(F1) %Retrieves the length of each simulation
for B = 1:F1(Z)
for L = 100:2601 % This is for the 2500 steps, and we repeat the functions from part 1
o(L) = L; % This keeps track of the time
d(L) = rand; %Retrieves a random value for the walker
if d(L) > .5
s(L) = s(L-1) + step1; %The walker steps to the right
elseif d(L) < .5
s(L) = s(L-1) - step1; %The walker steps to the left
else
s(L) = .5;
end
end
Endp(B) = s(L); %This stores the poisiton for each simulation
end
imp = sprintf('%g th Simulation of 2500 Steps', F(Z));
figure('Name', imp)
histogram(Endp()) %This plots the end point for each simulation
title(imp)
xlabel('The Position of the Walker')
ylabel('The Number of Time the Walker is in that Position')
grid on
end
In your code, you initialized
s(1) = 100;
therefore variable s has a single elememt. But then in your code you run
s(L) = s(L-1) + step1;
where L=100. This line try to access s(99) which does not exist and MATLAB throws the error.
how would i fix that? I appreciate your help. I am still a beginner in MATLAB
Omar, solving it depends on what you are trying to do in the code. If you got the code from someone else, then you should ask the author about this issue.
It is my code. I'm just trying to edit the step size, so it can be anywhere from 0 to 1. The error resides in the for loop. I'll try using the debugging tools on MATLAB. Thank you for your help though.

Sign in to comment.

Answers (1)

Rik
Rik on 29 Mar 2020
If you set a breakpoint just before that line you will notice the values of all your variables: s is a scalar and L has a value of 100. Then you try to do s(L-1), which will attempt to access the 99th position in s, which doesn't exist.
You should really try using the debugging tools, starting with the warnings mlint is giving you. Make sure to resolve those. If you are absolutely sure they don't apply to your specific situation you can right-click the orange underlined code and select the option to suppress the warning on that line. None of the warnings mlint is currently giving you should be ignored.
Matlab has very flexible debugging tools including breakpoints and the option to halt execution when an error has been thrown. Use them to your advantage.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 29 Mar 2020

Commented:

on 29 Mar 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!