How to compare class type of 2 variables to find equality and use it within switch-case

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)

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

No because i want to check for the class of a and b. and i cant just do switch c because that doesnt take e into account i need both class of a and b to be double in order for the statement to add a and b together
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
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
Thank You so much!!! thats all i needed to know. Stack side by side.

Sign in to comment.

Asked:

on 26 Sep 2015

Commented:

on 27 Sep 2015

Community Treasure Hunt

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

Start Hunting!