How to compare class type of 2 variables to find equality and use it within switch-case
Mostrar comentarios más antiguos
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
Respuestas (1)
James Tursa
el 26 de Sept. de 2015
Editada: James Tursa
el 26 de Sept. de 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 comentarios
Tab for Lab
el 26 de Sept. de 2015
James Tursa
el 26 de Sept. de 2015
Editada: James Tursa
el 26 de Sept. de 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
el 26 de Sept. de 2015
James Tursa
el 26 de Sept. de 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
el 27 de Sept. de 2015
Categorías
Más información sobre General Applications en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!