How do I change the function for certain points in a for loop

4 Ansichten (letzte 30 Tage)
Temi O
Temi O am 23 Feb. 2019
Kommentiert: Temi O am 24 Feb. 2019
time = 0: 0.1: 1000;
g(1) = 0
g= zeros (1, length (time))
for t= 1: length (time)- 1
g(t+1)= g(t)+ A
% So I want to change the formula for times: 100:100:600. Not sure of how to do this?
g(t+1)= g(t)+ B
end
I’ve tried for t= 101:100: 601, but I think this affects my code down the line. i’ve tried if t== 101:100:601 but this didn’t work either. Not sure of what ti do.

Akzeptierte Antwort

Brian Hart
Brian Hart am 23 Feb. 2019
I suggest making an "other function" variable that has the values for which you want to use the other function. Then do an IF test in the loop, using the MATLAB function ismember. For example...
time = 0: 0.1: 1000;
g(0) = 0
g= zeros (1, length (time))
otherFcnVals = [100:100:600]
for t= 1: length
if ismember(t, otherFcnVals)
g(t+1)= g(t)+ B
else
g(t+1)= g(t)+ A
end
end

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by