MATLAB: Switch case syntax need

switch

[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.

Best Answer

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