For Loop, Unequal Sizes Help

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

 Risposta accettata

Ameer Hamza
Ameer Hamza il 21 Set 2020
Modificato: Ameer Hamza il 21 Set 2020
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 Commenti

Thank you
Ameer Hamza
Ameer Hamza il 22 Set 2020
I am glad to be of help!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by