필터 지우기
필터 지우기

How can I fix "Attempted to access Y(0); index must be a positive integer or logical." MATLAB for loop error?

조회 수: 1 (최근 30일)
I'm trying to use a for loop in MATLAB and I keep getting an error message. The instructions I'm following are to create a 10000X1 vector using a random number generator and label it "e", and then a second 10000X1 vector that only contains zeros. Then using a loop I have to simulate the process Y(t)=(1-a)*Y(t-1)*e(t) where a=0.1. The code I have been attempting to run is:
Y=zeros(10000,1);
e=randn(10000,1);
for t=1,2,...,10000
a=0.1;
Y(t)=(1-a)*Y(t-1)*e(t);
end
I keep getting an error message that says: "Attempted to access Y(0); index must be a positive integer or logical." Any help is appreciated

답변 (1개)

Walter Roberson
Walter Roberson 2015년 9월 7일
for t=1,2,...,10000
is not valid syntax. Please post your actual code.
Your formula has
Y(t)=(1-a)*Y(t-1)*e(t);
when t = 1, the Y(t-1) part is going to be trying to access Y(0). In MATLAB you cannot index an array at location 0.
I suspect the formula you need is
Y(t+1) = (1-a)*Y(t)*e(t);
with the "for" running from 1 to 9999. However, it is possible that instead you want the formula that you have now but with t starting from 2 in the loop.
The difference between the two formulas has to do with whether e(1) should ever be used and if so whether it should be used with Y(1) or with Y(2)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by