how to write if Loop (or while loop) in this kind of problem

1 回表示 (過去 30 日間)
Asif Rashid
Asif Rashid 2021 年 3 月 13 日
コメント済み: Star Strider 2021 年 3 月 13 日
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

採用された回答

Star Strider
Star Strider 2021 年 3 月 13 日
I gave one example in a Comment to your earlier Question.
  2 件のコメント
Asif Rashid
Asif Rashid 2021 年 3 月 13 日
Thankyou Sir, i got some idea from that statement
f = @(x,NS,NL) ((x>=0.1) & (x<=0.3)).*NL + ((x>0.3) & (x<=0.5)).*NS;
but how to write it for 5 conditions as mentioned here in this question please.
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;
is this statement correct for above mentioned conditions.
in my case , i want the reference points of above mentioned limits.
for example
at
0.2 (in case of NL)
0.4 (in case of NS)
0.6 (in case of ZE)
0.8 (in case of PS)
1 (in case of PL)
Thankyou for your guidance , Sir
Star Strider
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 ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by