Variable updated in for loop iteration
Show older comments
I have a very simple code that do not update the variable in the array during the for loop interation. It works if I run the for loop as i = 1. In that cas I can see that revenue(1) and budget_i(2) report the correct values. When the for loop interation exceed 1 interation, the arrays revenue and budget_i are no longer updated.
close = [0.01110 0.03600 0.01810 0.0208 ... 0.06470 0.00120]; % originally 126 values
revenue = zeros(1,126);
budget_i = zeros(1,126);
budget_i(1) = 100;
for i = 1
revenue(i) = budget_i(i) +(budget_i(i)*close(i));
if close(i) > 0
budget_i(i+1) = revenue(i) + 100;
elseif close(i) < 0
budget_i(i+1) = revenue(i) + 200;
end
end
%% here I have, correcly, budget_i(1) = 100; revenue(1) = 101.11; budget(2) = 201.11.
at this point I was expecting the revenue(2) calculation to work perfectly using budget_i(2) that I defined in the previous iteration but all I get in the variable array is a zero. Also revenue(1) is zero.
The code should work with for loop as for i = length(close) but revenue(i) and budget_i(i) failed to update and all I get is two arrays as revenue = zeros(1,126) and budget_i = zeros(1,126) with budget_i(1) = 100.
Any idea why it doesn't work at iteration greater than 1?
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with MATLAB 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!