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

 采纳的回答

Ameer Hamza
Ameer Hamza 2020-9-21
编辑:Ameer Hamza 2020-9-21
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;

更多回答(0 个)

类别

帮助中心File 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