Index exceeds the number of array elements (1).

조회 수: 2 (최근 30일)
Bradley Kitzul
Bradley Kitzul 2020년 12월 22일
댓글: Bradley Kitzul 2020년 12월 23일
Hi, I am getting the same error as in the tittle. I am very new to coding and I do not know why I am getting it. I tried stepping through the code and it gets stuck on, i=3 b=3. Please help. My code is below and the alpha matrix is the attached picture. Furthermore, you can think of g(M) as just a constant. Thanks to everyone who helps! :D
a(1)=g(M);
for i=2:M
su = 0;
v(1) = 1;
for b=2:i
su =su - alpha(b, b-1)*v(b-1);
end
a(i)=g(M)*su;
end
a=fliplr(a);

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 22일
You initialize v(1) only, and no other v.
You use v(b-1) in the loop, which requires that you have more v available, such as if you were to be assigning to v(b) inside the for b loop.
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 12월 23일
When b becomes 3, so that v(b-1) is trying to access v(2), then what do you expect that v(2) value to be? Do you expect it to be something you had already initialized before the loop, or do you expect it to be something that you calculated during the previous iteration of the for b loop? If you are extending v as you go, then what is the formula for the new v ?
Bradley Kitzul
Bradley Kitzul 2020년 12월 23일
Here are the equations I am trying to implement. So yes it is calculated in the previous loop. Sorry if my question is rudimentary, I am new to this stuff! :D

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 12월 23일
Code it using the same variables as in the equations, or else you are going to get yourself confused.
v(i) = 1;
for m = i+1:M
v(m) = - sum(alpha(m, i:m-1) .* v(i:m-1));
end
a(i) = sum(g(i:M) .* v(i:M));

카테고리

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