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

1 Ansicht (letzte 30 Tage)
Asif Rashid
Asif Rashid am 13 Mär. 2021
Kommentiert: Star Strider am 13 Mär. 2021
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

Akzeptierte Antwort

Star Strider
Star Strider am 13 Mär. 2021
I gave one example in a Comment to your earlier Question.
  2 Kommentare
Asif Rashid
Asif Rashid am 13 Mär. 2021
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 am 13 Mär. 2021
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by