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

조회 수: 7 (최근 30일)
Temi O
Temi O 2019년 2월 23일
댓글: Temi O 2019년 2월 24일
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.

채택된 답변

Brian Hart
Brian Hart 2019년 2월 23일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by