MATLAB: How to change the function for certain points in a for loop

for loopif statementMATLAB

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.

Best Answer

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