How to use switch statement

3 Ansichten (letzte 30 Tage)
Alexya
Alexya am 14 Okt. 2022
Beantwortet: Matt J am 14 Okt. 2022
I need to create a function through switch statements that are from one bound to the next bound. How would i use the case to display bound1 through bound2. Every variable falling within the bounds is included in the case.
i have
switch n
case {bound1 through bound2}
  2 Kommentare
Matt J
Matt J am 14 Okt. 2022
Don't do that. Use logical indexing instead.
Alexya
Alexya am 14 Okt. 2022
This is what I have so far
function [lettergrade] = curvedGrade(grade,bound)
a = (bound(1):100);
b = (bound(2):bound(1));
c = (bound(3):bound(2));
d = (bound(4):bound(3));
switch grade
case{a}
lettergrade = 'A';
case{b}
lettergrade = 'B';
case{c}
lettergrade = 'C';
case{d}
lettergrade = 'D';
otherwise
lettergrade = 'F';

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 14 Okt. 2022
No, do it like this:
[lettergrade] = curvedGrade(71,[90,80,70,60, 50])
lettergrade = "C"
function [lettergrade] = curvedGrade(grade,bound)
gradeList=flip(["A" "B" "C" "D" "F"]);
idx=discretize(grade,[flip(bound(:));100]);
lettergrade=gradeList(idx);
end

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 14 Okt. 2022

https://www.mathworks.com/help/matlab/ref/double.discretize.html and switch on the bin number.

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by