Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Temi O
Temi O le 23 Fév 2019
Commenté : Temi O le 24 Fév 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.

Réponse acceptée

Brian Hart
Brian Hart le 23 Fév 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

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by