how to write if Loop (or while loop) in this kind of problem
1 次查看(过去 30 天)
显示 更早的评论
Dear Colleagues and teachers
i want to write
if NL is between 0.1 and 0.3 then output is NL (eactly at 0.2)
else if
NS is between 0.31 and 0.5 then output is NS (eactly at 0.4)
i just want to know the structure of this kind of if loop
---------------------------------------------
i have these limits and i want to write if loop to execute these statements.
NL [0.1 0.2 0.3]
NS [0.31 0.4 0.5]
ZE [0.51 0.6 0.7]
PS [0.71 0.8 0.9]
PL [0.91 1 1.1]
Looking forward for your kind help please. thank you
0 个评论
采纳的回答
Star Strider
2021-3-13
2 个评论
Star Strider
2021-3-13
Thjat appears to me to be correct, however it may be necessary first to define:
NL = 0.2;
NS = 0.4;
ZE = 0.6;
PS = 0.8;
PL = 1;
Then the complete code is:
f = @(x,NS,NL,ZE,PS,PL) ((x>=0.1) & (x<=0.3)).*NL + ((x>0.3) & (x<=0.5)).*NS + ((x>0.51) & (x<=0.7)).*ZE + ((x>0.71) & (x<=0.9)).*PS + ((x>0.9) & (x<=1.1)).*PL;
NL = 0.2;
NS = 0.4;
ZE = 0.6;
PS = 0.8;
PL = 1;
x = linspace(-1, 2);
figure
plot(x, f(x,NS,NL,ZE,PS,PL), 'LineWidth',1.5)
grid
and it appears to produce the correct result.
Check the plot to see if it does what you want it to do.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!