Switch statement using intervals.
5 views (last 30 days)
Show older comments
i have a homework question which aska to solve same question using if as well as switch commands. it has specific intervals as shown below. Got done with if but dont know how to put these intervals in switch. Write a MATLAB function that determines type of soil by taking soil particle size as input. For this question,
a) use if – elseif - else statement.
b) use switch – case statement.
You may refer to following soil classification table:
soil type lower bound(mm) upper bound(mm)
boulders 200 -
cobbles 60 -200
gravel 2 -60
sand 0.06- 2
silt 0.002- 0.06
clay - 0.002.
3 Comments
Answers (3)
Guillaume
on 16 Nov 2016
This does not answer your question but may be helpful to others. The proper way of doing this in matlab would be:
sizes = [0.001 0.03 0.5 0.7 1.5 3 45 70 105 210 250];
discretize(sizes, [-Inf 0.002 0.06 2 60 200 Inf] , 'categorical', {'clay', 'silt', 'sand', 'gravel', 'cobbles', 'boulders'})
No loops, if or switch statements required.
2 Comments
Thomas Michiels
on 18 Sep 2018
if i was the question writer, i would be happy with this answer, getting something better then i asked for. This should be the accepted answer as the other answers also don't give a solution, only as why doing so is anti idiomatic and should be avoided
Walter Roberson
on 18 Sep 2018
Edited: Walter Roberson
on 18 Sep 2018
Thomas, I gave an actual solution. It involved changing one word, x to true . We discussed why it wasn't a preferred solution, but it was still a solution. The user also explained why using discretize() was not suitable for their purposes, but that my solution was suitable for them.
Walter Roberson
on 16 Nov 2016
In your code, change
switch x
to
switch true
and make sure to give an Otherwise.
Using a logical constant for the switch and using logical expressions for the cases is obscure but it is defined.
6 Comments
Guillaume
on 3 Dec 2017
As evidence that the syntax is very obscure and unusual (I actually had to look the documentation of switch when Walter first posted his answer), you only have to look at the first tip in the tip section of switch documentation:
A case_expression cannot include relational operators such as < or > for comparison against the switch_expression.
Yet, here we are with a case expression which include a relational operator:
case bw<=1.4e6
It's actually an interesting dilemma, one part of switch documentation allows the above construct, yet another part disallows it. So, I'm not even sure that Walter's statement that "it is defined" is actually correct. Something to discuss with Mathworks...
As for not requiring comments, you're of course free to do what you want. In my opinion, more comments than necessary is always better than not enough. In this particular case, I would argue that anybody who comes across switch true and doesn't immediately starts to wonder what is going on either is a true matlab expert or doesn't understand what they're reading.
As Walter implies, in most other languages, the current construct is not even allowed. A normal switch consists of switch expression_whose_value_is_unknown ... case _constant_expression, often simplified to switch variable_name ... case _constant. And basically says, pick whichever case constant corresponds to the value of the switch variable. Here we're turning it on its head and say pick whichever case variable is equal to the switch constant.
Walter Roberson
on 3 Dec 2017
"A case_expression cannot include relational operators such as < or > for comparison against the switch_expression."
This situation does not do that: the comparisons are against other expressions. And the tip is not correct:
var = true;
switch var
case var > false
disp('dat!')
otherwise
disp('?')
end
It is just hardly ever useful to use relational operators in the cases to test against the switch expression, because the use of relational operators in the cases can only work if the switch expression was logical or 0 or 1 (because equality tests are used)
So...
>> var = 1;
switch var
case var > 0
disp('dat!')
otherwise
disp('?')
end
dat!
but you run into odd situations like
>> var = 0; switch var
case var == 0
disp('var 0')
otherwise
disp('var 0 false')
end
var 0 false
>> var = 1; switch var
case var == 0
disp('var 0')
otherwise
disp('var 0 false')
end
var 0 false
In the first case with var = 0, var == 0 is true, but true is 1 and 1 ~= 0 (the switch expression value) so the first case is not met. In the second case with var = 1, var == 0 is false, and false is 0 and 0 ~= 1 (the switch expression value) so the second case is not met.
And then you get
>> var = 0; switch var
case var ~= var
disp('var is not equal to itself')
otherwise
disp('var is equal to itself')
end
var is not equal to itself
0 ~= 0 is of course false, which is 0, but 0 is equal to the switch expression value of 0, so the case is considered true!
Petri, although perhaps you might glance at that last example and find it so obvious that it does not even need a comment, I tell you that the rest of us need a comment for it!
Bruno Luong
on 18 Sep 2018
edges=[-Inf 0.002 0.06 2 60 200 Inf];
[~,intnum] = histc(x,edges);
switch intnum
case 1 % clay
processclay(x)
case 2 % silt
...
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!