How to compare class type of 2 variables to find equality and use it within switch-case
Show older comments
I am trying to make a statement using switch case block and I want to know how thats possible with 2 variables?
I am supposed to add variables a and b if the case is 'double' but how would I do that if:
a = [1 2 3 4]
b = [2 1 3 1] ? So far I was thinking of doing this.
a = [1 2 3 4]
b = [2 1 3 1]
c = class (a)
d = class (b)
e = c == d
switch blah
case 'double'
a + b
case 'logical'
a & b
otherwise
disp ('None')
end
and so on
Now I need a switch and case block to create a scenario where if our switch x has the case 'double' (class type) it will add variables a and b.
Any help will be much appreciated. Thank You
Answers (1)
James Tursa
on 26 Sep 2015
Edited: James Tursa
on 26 Sep 2015
What if you simply switched on the value of c? E.g.,
switch c
Does this do what you want? Or is there something else you need? Maybe add a check to see if a and b are the same class, e.g. check that isequal(c,d) is true.
5 Comments
Tab for Lab
on 26 Sep 2015
James Tursa
on 26 Sep 2015
Edited: James Tursa
on 26 Sep 2015
That's why I suggested adding a check for that using isequal(c,d). E.g., something like this
if( isequal(c,d) )
switch c
case 'double'
a + b
case 'logical'
a & b
otherwise
disp('None')
end
else
error('Classes must match');
end
Tab for Lab
on 26 Sep 2015
James Tursa
on 26 Sep 2015
You could stack the classes side by side. E.g.,
switch [c,d]
case 'doubledouble'
a + b
case 'logicallogical'
a & b
otherwise
disp('None')
end
Tab for Lab
on 27 Sep 2015
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!