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"?

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 21 Sep 2020
Modifié(e) : Ameer Hamza le 21 Sep 2020

0 votes

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 commentaires

Ryan Griffith
Ryan Griffith le 21 Sep 2020
Thank you
Ameer Hamza
Ameer Hamza le 22 Sep 2020
I am glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by