Filter löschen
Filter löschen

combining multiple or statements

3 Ansichten (letzte 30 Tage)
shru s
shru s am 12 Jul. 2017
Kommentiert: shru s am 12 Jul. 2017
hello, i wish to combine this or statement in in one line
switch m
case m==1|2|3|4|5|6|7|8|9|10
disp('A');
end
Is this possible? I do not want to type 1 2 3 4 etc because i have a lot of values to type in. Thank you

Akzeptierte Antwort

Rik
Rik am 12 Jul. 2017
As the documentation for switch suggests, making a cell array enables you to check multiple conditions at once. You can open the documentation by typing doc switch or by using Google.
m=3;
switch m
case {1,2,3,4}
disp('boo!')
otherwise
disp('ah!!')
end
You can also use a variable instead of typing them out.
m=3;
c=num2cell(1:10);
switch m
case c
disp('boo!')
otherwise
disp('aw..')
end
  2 Kommentare
shru s
shru s am 12 Jul. 2017
Brilliant! Thank you so much! :) if i run it for three loops and i get boo! boo! aw.. is the 1st 2nd and 3rd loop respectively, is there a way i can concatenate them together and display them? like boo!boo!aw..
shru s
shru s am 12 Jul. 2017
ive got it. thank you :)

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by