Changing the parameters of a for loops if condition is met
4 views (last 30 days)
Show older comments
Hi All,
was wondering if you could shed some light on an issue, i have read other breaking of for loops but got lost with the explanation or size of the code in the example. I have a simplified version of my code below which is for running total temperature rise.
NOC =2; % number of cycles
RT=zeros(100,NOC); % running temp total
RT(1,1)=20 % starting ambient temp
for Y = 1:NOC
for x = 1:100
if x==1 && Y>1
RT(x,Y)=RT(end,Y-1);
end
value(x) = 2*x;
if x ~=1
RT(x,Y) = value(x) +RT(x-1,Y)
end
end
end
I want to add in a temperature parameter which if met, changes to a different line of code and continues, so if the RT value is greater than 1000, stop multiplying it by 2 and start multiplying it by 5 etc. I've seen as much as its not as easy to break a for loop than others but when i try, if,break and continue i am doing something wrong and it is not seeing the condition i have set as met to execute the different line of code.
Thank you in advance for any help.
3 Comments
dpb
on 1 Apr 2018
Actually, for RT<=50 you have both branches being taken. Superficial read indicates that doesn't really matter for this specific code; it just overwrites the previous answer, but is dangerous if something else were to change.
Accepted Answer
dpb
on 1 Apr 2018
...
Q=1 % heating of 1kW
QMult=[2 5]; % choices from original posting
for Y = 1:NOC
for x = 1:100
if x ==1 &&Y>1
RT(x,Y) =RT(end,Y-1);
end
if x ~=1
QFact=Qmult(RT(x,Y)>50 +1); % lookup desired factor from previous temp
TR(x,Y) = Q/(SHW*FR)*QFact;
end
end
end
...
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!