Help with Inequalities In Switch Statement

8 visualizaciones (últimos 30 días)
Scott Banks
Scott Banks el 22 de Jul. de 2025
Editada: the cyclist el 22 de Jul. de 2025
Star Strider ha marcado con alerta este/a pregunta
Dear all,
I am making a function that ivolves using the switch statements. I want to display a certain data set using the inequalitiy operators.
Here is my code:
function [Inflex] = inflection_point
Inflex01 = [-0.35;-0.10;0.10;0.30;0.50;0.75;1.20];
Inflex02 = [-0.50;0.15;0.25;0.35;0.45;0.60;0.95];
Inflex03 = [0.10;0.25;0.30;0.40;0.45;0.55;0.85];
Inflex04 = [0.20;0.30;0.35;0.40;0.45;0.50;0.80];
Inflex05 = [0.20;0.35;0.40;0.40;0.45;0.50;0.75];
Inflex06 = [0.25;0.35;0.40;0.45;0.45;0.50;0.70];
Inflex07 = [0.30;0.35;0.40;0.45;0.45;0.50;0.70];
Inflex08 = [0.30;0.40;0.45;0.45;0.45;0.50;0.65];
Inflex09 = [0.35;0.40;0.40;0.45;0.45;0.50;0.65];
Inflex1 = [0.35;0.40;0.40;0.45;0.45;0.50;0.65];
Inflex2 = [0.40;0.45;0.45;0.50;0.50;0.50;0.55];
Inflex3 = [0.45;0.45;0.45;0.50;0.50;0.50;0.55];
Inflex4 = [0.40;0.50;0.50;0.50;0.50;0.50;0.55];
Inflex5 = [0.40;0.50;0.50;0.50;0.50;0.50;0.55];
Storey = ['7';'6';'5';'4';'3';'2';'1'];
Khat01 = [Inflex01];
Khat02 = [Inflex02];
Khat03 = [Inflex03];
Khat04 = [Inflex04];
Khat05 = [Inflex05];
Khat06 = [Inflex06];
Khat07 = [Inflex07];
Khat08 = [Inflex08];
Khat09 = [Inflex09];
Khat1 = [Inflex1];
Khat2 = [Inflex2];
Khat3 = [Inflex3];
Khat4 = [Inflex4];
Khat5 = [Inflex5];
Tb = table(Storey,Khat01,Khat02,Khat03,Khat04,Khat05,Khat06,Khat07,Khat08,Khat09,Khat1,Khat2,Khat3,Khat4,Khat5)
Khat = input('Enter a value of Khat: ')
switch Khat
case (Khat >= 0) && (Khat <= 0.14)
disp(Tb(:,2))
case 0.2
disp(Tb(:,3))
case 0.3
disp(Tb(:,4))
case 0.4
disp(Tb(:,5))
case 0.5
disp(Tb(:,6))
case 0.6
disp(Tb(:,7))
case 0.7
disp(Tb(:,8))
case 0.8
disp(Tb(:,9))
case 0.9
disp(Tb(:,10))
case 1
disp(Tb(:,11))
case 2
disp(Tb(:,12))
case 3
disp(Tb(:,13))
case 4
disp(Tb(:,14))
case 5
disp(Tb(:,15))
end
end
Take notice of the first case in the switch statement. If a value of Khat lies between 0 and 0.14, I want to display the second column from the table Tb. However, it will not display this column from the table. If I type say 0.07 in the input, I just get a return of the value 0.07 in the command window. For the other cases that have a single value (0.2,0.3,0.4 etc...) I do get the display I want. So, I'm just wondering why the inequalities are not working? In the end I will put inequalities in every case in the switch statement.
I'm sure you guys will have the answer!
Many thanks,
Scott
  1 comentario
Stephen23
Stephen23 el 22 de Jul. de 2025
Editada: Stephen23 el 22 de Jul. de 2025
"So, I'm just wondering why the inequalities are not working?"
They are working: SWITCH compares the value of the expression (in your case KHAT) against the values given for each CASE. When your provide KHAT = 0.7 then the first case evaluates to either TRUE or FALSE, i.e. 1 or 0 respectively. Is 0.7 equal to either 1 or 0 ? Hint: no.
Lets try your approach right now:
Khat = 0.7
Khat = 0.7000
(Khat >= 0) && (Khat <= 0.14)
ans = logical
0
switch 0.7
case false % this is what you are hiding behind some formula
disp('this will not work')
otherwise
disp('because 0.7 ~= 0')
end
because 0.7 ~= 0
When you look at your formula it is clear that neither Khat=0 nor Khat=1 will ever match the CASE expression. So there are absolutely no values for which that first case will ever be evaluated.
"In the end I will put inequalities in every case in the switch statement."
Much better: use a matrix and indexing.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 22 de Jul. de 2025
Editada: Stephen23 el 22 de Jul. de 2025
Your SWITCH statement will not work like that. But really, you should avoid SWITCH for this task (unless you really want to write significantly more code than you need to and also want to force yourself into code that is harder to maintain or generalise).
Avoid having lots and lots of numbered variables: that is a dead-end that you really do not want to go down.
Make your own life easier by using arrays and indexing:
M = [-0.35, -0.5, 0.1, 0.2, 0.2, 0.25, 0.3, 0.3, 0.35, 0.35, 0.4, 0.45, 0.4, 0.4; -0.1, 0.15, 0.25, 0.3, 0.35, 0.35, 0.35, 0.4, 0.4, 0.4, 0.45, 0.45, 0.5, 0.5; 0.1, 0.25, 0.3, 0.35, 0.4, 0.4, 0.4, 0.45, 0.4, 0.4, 0.45, 0.45, 0.5, 0.5; 0.3, 0.35, 0.4, 0.4, 0.4, 0.45, 0.45, 0.45, 0.45, 0.45, 0.5, 0.5, 0.5, 0.5; 0.5, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.5, 0.5, 0.5, 0.5; 0.75, 0.6, 0.55, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5; 1.2, 0.95, 0.85, 0.8, 0.75, 0.7, 0.7, 0.65, 0.65, 0.65, 0.55, 0.55, 0.55, 0.55]
M = 7×14
-0.3500 -0.5000 0.1000 0.2000 0.2000 0.2500 0.3000 0.3000 0.3500 0.3500 0.4000 0.4500 0.4000 0.4000 -0.1000 0.1500 0.2500 0.3000 0.3500 0.3500 0.3500 0.4000 0.4000 0.4000 0.4500 0.4500 0.5000 0.5000 0.1000 0.2500 0.3000 0.3500 0.4000 0.4000 0.4000 0.4500 0.4000 0.4000 0.4500 0.4500 0.5000 0.5000 0.3000 0.3500 0.4000 0.4000 0.4000 0.4500 0.4500 0.4500 0.4500 0.4500 0.5000 0.5000 0.5000 0.5000 0.5000 0.4500 0.4500 0.4500 0.4500 0.4500 0.4500 0.4500 0.4500 0.4500 0.5000 0.5000 0.5000 0.5000 0.7500 0.6000 0.5500 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.2000 0.9500 0.8500 0.8000 0.7500 0.7000 0.7000 0.6500 0.6500 0.6500 0.5500 0.5500 0.5500 0.5500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
E = [0,0.14,0.2:0.1:1,2:5];
N = 0.07;
B = discretize(N,E)
B = 1
M(:,B)
ans = 7×1
-0.3500 -0.1000 0.1000 0.3000 0.5000 0.7500 1.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The power of MATLAB is in using arrays and indexing (it is even in the name). Use arrays and indexing. If you are not using arrays and indexing then you are not learning how to use MATLAB.
  1 comentario
Scott Banks
Scott Banks el 22 de Jul. de 2025
Thanks, Stephen, that becomes much simpler. Thank you for that.

Iniciar sesión para comentar.

Más respuestas (3)

dpb
dpb el 22 de Jul. de 2025
See the Tips section of switch, case, otherwise that notes
Tips
  • A case_expression cannot include relational operators such as < or > for comparison against the switch_expression. To test for inequality, use if, elseif, else statements.
Seems like an unecessarily restrictive rule, but that's the way it is...can't use switch here.
However, you can probably use interp1 and avoid the construct altogether.
  1 comentario
Steven Lord
Steven Lord el 22 de Jul. de 2025
You could use the discretize function. In the edges vector below, the first bin catches any values less than 0. The last bin catches everything 5 or greater.
edges = [-Inf, 0 0.14 0.2:0.1:1 2:5 Inf]
edges = 1×17
-Inf 0 0.1400 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 2.0000 3.0000 4.0000 5.0000 Inf
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sampleKhat = 0.45
sampleKhat = 0.4500
whichBin = discretize(sampleKhat, edges)
whichBin = 6
fprintf("%g is in bin %d, between values %g and %g in edges.", ...
sampleKhat, whichBin, edges(whichBin), edges(whichBin+1))
0.45 is in bin 6, between values 0.4 and 0.5 in edges.

Iniciar sesión para comentar.


the cyclist
the cyclist el 22 de Jul. de 2025
Editada: the cyclist el 22 de Jul. de 2025
What is happening in the first case of your switch statement is not that it checks whether
(Khat >= 0) && (Khat <= 0.14)
is true or not. It is evaluating that expression -- it is equal to "true" or "1" -- and then seeing if the input value of Khat equals that (which it does not). That is why it skips over that case.

Walter Roberson
Walter Roberson el 22 de Jul. de 2025
To use inequalities in case statements, you need to provide a logical value to the switch statement. For example,
switch true
case (Khat >= 0) && (Khat <= 0.14)
case Khat == 0.2
end
Note that even if you avoid this situation by using if beforehand, you still run into the issue that you should rarely compare floating point numbers for exact equality. Two floating point numbers derived from different sources might have different low-order bits. For example, if you read in a 0.2 from an input() statement, there is no certainty that it will be bit-for-bit identical to a literal 0.2 in the case statement. Remember that MATLAB does not use decimal representation for fractional numbers and that most multiples of 0.1 cannot be exactly represented in floating point binary formats.
You would be safer using
switch round(Khat*10)
case 2
case 3
...
case 10
case 20
...
end

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by