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!