Info
此问题已关闭。 请重新打开它进行编辑或回答。
Can anyone help me to change this if else looping into for looping ?
1 次查看(过去 30 天)
显示 更早的评论
maxit = 1000;
wmax = 1.2;
wmin = 0.4;
for it=1:maxit
if it <= 75
w = wmax+(-1*(wmax-wmin)*it/75);
elseif it <= 150
w = wmin+(1*(wmax-wmin)*(it-75)/150); % jarak 75
elseif it <= 225
w = wmax+(-1*(wmax-wmin)*(it-75)/150);
elseif it <= 290
w = wmin+(1*(wmax-wmin)*(it-225)/215); % jarak 65
elseif it <= 355
w = wmax+(-1*(wmax-wmin)*(it-140)/215);
elseif it <= 410
w = wmin+(1*(wmax-wmin)*(it-355)/270); % jarak 55
elseif it <= 465
w = wmax+(-1*(wmax-wmin)*(it-195)/270);
elseif it <= 510
w = wmin+(1*(wmax-wmin)*(it-465)/315); % jarak 45
elseif it <= 555
w = wmax+(-1*(wmax-wmin)*(it-240)/315);
elseif it <= 590
w = wmin+(1*(wmax-wmin)*(it-555)/350); % jarak 35
elseif it <= 625
w = wmax+(-1*(wmax-wmin)*(it-275)/350) ;
elseif it <= 650
w = wmin+(1*(wmax-wmin)*(it-625)/375); % jarak 25
elseif it <= 675
w = wmax+(-1*(wmax-wmin)*(it-300)/375);
elseif it <= 690
w = wmin+(1*(wmax-wmin)*(it-675)/390); % jarak 15
elseif it <= 705
w = wmax+(-1*(wmax-wmin)*(it-315)/390);
end
end
1 个评论
回答(1 个)
Walter Roberson
2017-12-4
It is already for looping. if/else is not looping.
You could rewrite like,
for it = 1 : 75
w = wmax+(-1*(wmax-wmin)*it/75);
end
for it = 76 : 150
w = wmin+(1*(wmax-wmin)*(it-75)/150); % jarak 75
end
for it = 151 : 225
w = wmax+(-1*(wmax-wmin)*(it-75)/150);
end
and so on. There are other ways to write the code as well.
1 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!