Code with Switch case and if

조회 수: 67 (최근 30일)
Andrea Miceli
Andrea Miceli 2021년 3월 31일
댓글: Andrea Miceli 2021년 4월 1일
Hello to evreybody, i am not able to figure out why this code is not working. When I run the code it always give me back that the grade is '5' even if the points are >9.
For example, I tried to change the 'case' condition and if use {40,41,42,43,44,45,46,47,48,49,50} it gives me back the right grade if I use '(points >= 40 && points <= 50)' it doesn't work as I want. Can somebody tell me how to fix the condition for the switch case?
Thank you in advance for your help!
%code
points=randi([-5,55]);
if (points<0) || (points>50)
grade = 'NA';
return
end
switch points
case (points >= 40 && points <= 50)
grade='1';
case (points>=30 && points<=39)
grade='2';
case (points>=20 && points<=29)
grade='3';
case (points>=10 && points<=19)
grade='4';
otherwise (points>=0 && points<=9)
grade='5';
end

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 3월 31일
편집: Cris LaPierre 2021년 3월 31일
A switch statement looks for the case that is true. The conditional for each of your cases returns a true/false value. This means you switch on true, not points.
switch true
case (points >= 40 && points <= 50)
grade='1';
case (points>=30 && points<=39)
grade='2';
case (points>=20 && points<=29)
grade='3';
case (points>=10 && points<=19)
grade='4';
otherwise (points>=0 && points<=9)
grade='5';
end
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2021년 3월 31일
편집: Cris LaPierre 2021년 3월 31일
If you prefer to use points for your switch, then use num2cell to set multiple values for each case statement.
switch points
case num2cell(40:50)
grade='1';
case num2cell(30:39)
grade='2';
...
end
One caution about this approach is that points must exactly match a defined value to work. For example, if points=44.5, the assigned grade will be '5' because 44.5 is not one of the defined values (40, 41 42,...,50).
Andrea Miceli
Andrea Miceli 2021년 3월 31일
thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 3월 31일
Unless this is part of a homework assignment where you're required to use switch / case, I'd use a different tool. I'd use logical indexing or discretize which have an added benefit of being vectorized.
points=randi([-5,55], 10, 1);
grade1 = repmat("NA", size(points));
% logical indexing
case1 = (points >= 40) & (points <= 50);
grade1(case1) = "1";
case2 = (points >= 30) & (points <= 39);
grade1(case2) = "2";
case3 = (points >= 20) & (points <= 29);
grade1(case3) = "3";
case4 = (points >= 10) & (points <= 19);
grade1(case4) = "4";
case5 = (points >= 0) & (points <= 9);
grade1(case5) = "5";
% discretize
boundaries = [-Inf, 0, 10, 20, 30, 40, 50];
% anything in the range [boundaries(1), boundaries(2)) gets grades(1)
% anything in the range [boundaries(2), boundaries(3)) gets grades(2)
% etc
grades = ["NA", "5", "4", "3", "2", "1"];
grade2 = discretize(points, boundaries, grades);
results = table(points, grade1, grade2, ...
'VariableNames', ["Raw points", "Logical indexing", "Discretize"])
results = 10×3 table
Raw points Logical indexing Discretize __________ ________________ __________ 11 "4" "4" 29 "3" "3" 15 "4" "4" 5 "5" "5" 23 "3" "3" 36 "2" "2" 14 "4" "4" 51 "NA" <missing> -2 "NA" "NA" 37 "2" "2"
Handling of the right boundary of the last bin in the discretize case requires a little bit of special treatment as does handling of values that are greater than the limit of a grade of 1. I'll leave that as an exercise to you, based on the documentation of the discretize function.
  댓글 수: 1
Andrea Miceli
Andrea Miceli 2021년 4월 1일
I was required to use switch/case. Btw your logical operation is very useful. I will practice this for sure! Thank you for your time.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Inertias and Loads에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by