필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

index exceeds matrix dimensions

조회 수: 2 (최근 30일)
Gigglemello
Gigglemello 2017년 3월 16일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (1개)

Image Analyst
Image Analyst 2017년 3월 16일
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)

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by