Code with Switch case and if
36 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
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).
その他の回答 (1 件)
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"])
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.
参考
カテゴリ
Help Center および File Exchange で Downloads についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!