[EDIT: Wed May 25 04:59:27 UTC 2011 - Reformat - MKF]
I want a switch build a switch case statement as follows:
switch value
case {10-100}
disp('Method is within 100')
case {101-500}
disp('Method is within 500')
otherwise
disp('Unknown method.')
end
my problem is how to set case statement to check within the range 10 to 100 or 101 to 500. Thanks in advance.

 Respuesta aceptada

Matt Fig
Matt Fig el 25 de Mayo de 2011

0 votos

Why would you want to do this with a SWITCH instead of an IF-ELSEIF structure?
N = 131;
switch ((N>=10 && N<=100) + 2*(N>=101 && N<=500))
case 1
disp('Method is within 100')
case 2
disp('Method is within 500')
otherwise
disp('Unknown method.')
end
The following approach seems more natural to me, and is more efficient if used many times with an even distribution of cases. This is because the first conditional will be the only one evaluated when it is true, but in the SWITCH statement both conditionals are evaluated every time.
if (N>=10 && N<=100)
disp('Method is within 100')
elseif (N>=101 && N<=500)
disp('Method is within 500')
else
disp('Unknown method.')
end

2 comentarios

Walter Roberson
Walter Roberson el 25 de Mayo de 2011
I'm not sure that it is true that each case is evaluated every time, but I cannot check at the moment.
I know that if you have multiple matching case labels, only the first will performed.
Mohammad Golam Kibria
Mohammad Golam Kibria el 25 de Mayo de 2011
Thanks

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 25 de Mayo de 2011

0 votos

switch value
case num2cell(10:100)
disp('Method is within 100')
case num2cell(101:500)
disp('Method is within 500')
otherwise
disp('Unknown method.')
end

4 comentarios

Mohammad Golam Kibria
Mohammad Golam Kibria el 25 de Mayo de 2011
your code works if my value is a natural number not for real number.Thanks
Walter Roberson
Walter Roberson el 25 de Mayo de 2011
If you want to handle real numbers, then why do you use the "otherwise" behavior for (value>100 & value<101) ? When we see obvious natural numbers in use, we are justified in providing a solution that only works for natural numbers.
Anyhow, here's the alternative:
switch true
case value >=10 && value <=100
disp('Method is within 100')
case value >=101 && value<=500
disp('Method is within 500');
otherwise
disp('Unknown method.')
end
david feilacher
david feilacher el 4 de Sept. de 2018
genius !!
Walter Roberson
Walter Roberson el 4 de Sept. de 2018
Note that for (value > 100 & value < 101) that you would hit the Otherwise. I would suggest that the problem was not created properly for real values:
switch true
case value >=10 && value <=100
disp('Method is within 100')
case value >100 && value<=500
disp('Method is within 500');
otherwise
disp('Unknown method.')
end

Iniciar sesión para comentar.

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Mayo de 2011

Comentada:

el 4 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by