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

 Accepted Answer

Ameer Hamza
Ameer Hamza on 21 Sep 2020
Edited: Ameer Hamza on 21 Sep 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 Comments

Thank you
I am glad to be of help!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!