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

7 visualizaciones (últimos 30 días)
Temi O
Temi O el 23 de Feb. de 2019
Comentada: Temi O el 24 de Feb. de 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.

Respuesta aceptada

Brian Hart
Brian Hart el 23 de Feb. de 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by