Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

index exceeds matrix dimensions

2 Ansichten (letzte 30 Tage)
Gigglemello
Gigglemello am 16 Mär. 2017
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
Hello, I've tried to do a reflecting random walk simulator 1d. I've did it first without the barrier and it graphs perfectly. However, when I tried adding reflecting barrier to it, it kept on producing the "index exceeds matrix dimension". Do you know what is causing the error? Thank you.
% This program will compute and plot the paths of a set of many
% random walkers, which are confined by a pair of barriers at +B and -B.
% Assume all walkers start at z = 0
nwalks = 10;
nsteps = 100;
figure;
hold on;
for j = 1:nwalks
x = randn(nsteps,1);
z = zeros(nsteps,1);
for k = 1:nsteps
% Reflecting barrier
B = 3;
if z(k+1) > B
z(k+1) = B - abs(B - z(k+1));
elseif z(k+1) < (-B)
z(k+1) = (-B) + abs((-B) - z(k+1));
end
z(k+1) = z(k) + x(k);
end
% Plot
plot(z);
xlabel('Time (step number)'), ylabel(' Distance');
hold off;
end

Antworten (1)

Image Analyst
Image Analyst am 16 Mär. 2017
z has nsteps elements. k goes from 1 to nsteps. But in the loop you are trying to assign z(k+1), in other words, z(nsteps + 1). Try k = 1:(nsteps-1)

Diese Frage ist geschlossen.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by