Hello,
I am having trouble with a loop. When I do the calculation using only one iteration it seems to work perfecty, but as soon as I try inputting it as a loop and changing the corresponding values the loop fails. Any ideas or help is greatly appreciated. The first part of the code is just to set it up the problem.
load('boulder.mat')
b_droptime = droptime;
b_t = t;
b_y = y;
b_dydyo = ones(631,186);
b_dydvo = b_t;
b_dydg = 0.5*(b_t.^2);
W = eye(631);
X = zeros(186,3);
for c = 1:186
H = [b_dydyo(:,c), b_dydvo(:,c), b_dydg(:,c)];
Y = b_y(:,c);
X(c,:) = (((H.')*W*H)\((H.')*W*Y));
end
This is the part of the code I am struggling with below
theo_y_B = zeros(631,186);
for up = 1:186
theo_y_B(up) = X(up,1) + X(up,2) * b_t(:,up) + 0.5 * X(up,3) * b_t(:,up).^2;
end
Whereas if I do just
theo_y_B = X(1,1) + X(1,2) * b_t(:,1) + 0.5 * X(1,3) * b_t(:,1).^2;
it works.
What do I not understand about "for loops"?

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 21일
편집: Ameer Hamza 2020년 9월 21일

0 개 추천

Change the line
from
theo_y_B(up) = X(up,1) + X(up,2) * b_t(:,up) + 0.5 * X(up,3) * b_t(:,up).^2;
to
theo_y_B(:, up) = X(up,1) + X(up,2) * b_t(:,up) + 0.5 * X(up,3) * b_t(:,up).^2;

댓글 수: 2

Ryan Griffith
Ryan Griffith 2020년 9월 21일
Thank you
Ameer Hamza
Ameer Hamza 2020년 9월 22일
I am glad to be of help!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 9월 21일

댓글:

2020년 9월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by