Homework Help: Conditional operator if-else-elseif-end with switch-case combined

1. Jun 19, 2014

Maylis

1. The problem statement, all variables and given/known data
#1 in the attachment

2. Relevant equations

3. The attempt at a solution
My code is working for all of the numeric, logical, character portions. I got 7/8 points
Code (Text):
if ischar(X_input)
Y_output = upper(X_input);
elseif isnumeric(X_input)
switch isnumeric(X_input)
case X_input < -2
Y_output = -1./((X_input.^2) + 1);
case (-2 <= X_input) & (X_input < -1)
Y_output = sin(X_input);
case (-1 <= X_input) & (X_input < 1)
Y_output = X_input.^2;
case (1 <= X_input) & (X_input < 2)
Y_output = 2;
case X_input >= 2
Y_output = 1./(X_input + 1);
end
elseif islogical(X_input)
Y_output = logical(~X_input);
else
Y_output = NaN;
end
The part that I believe is wrong is the else Y_output = NaN. I don't know what to use to test because it seems like everything is logical, numerical, or a character. So I guessed an array. I tested with an array and I get no output (not NaN). Is something wrong with my synthax?

Attached Files:

• CDLassignment.pdf
File size:
94.8 KB
Views:
138
2. Jun 19, 2014

AlephZero

The functions ischar, isnumeric, isLogical test the type of a variable, not its value. NaN is a numerical value, not a type of data. So the value of "isnumeric(NaN)" is "true".

NaN doesn't match any of the cases in the switch statement, so Y_output isn't set to anything.

Note, your switch statement "works" but it's not really what it is intended for. Look at the examples in http://www.mathworks.co.uk/help/matlab/ref/switch.html. It would be better to replace the switch with another if ... elseif .... else ...end statement.

3. Jun 19, 2014

D H

Staff Emeritus
The part you got wrong is the numerical part. The assignment says (emphasis mine) "for each element of X_input, assign to the corresponding element of Y output having the same index, a value based on the following piecewise function ..." You didn't do that.

For example, if X_input contained [3.0, 1.5, -3, 1.57079632679, 0.5], the output should have been [0.25, 2.0, -0.1, 0.0, 0.25].

4. Jun 19, 2014

Maylis

So I should not use the switch case otherwise block for this problem at all? I don't understand how to use the if-elseif-else-end block with all my various cases. It seemed natural to use a switch case because of all of the cases in the piecewise function. Should I do an elseif for every case in the piecewise function?

Last edited: Jun 19, 2014
5. Jun 19, 2014

AlephZero

Yes. You can "nest" a complete if .... elseif ... elseif .... else ... end structure inside another one, which is what I would have done here.

The outer one does the tests for character, numeric, and logical. The inner one tests the different ranges.

Actually I'm surprised your code even compiles. It could be that it actually means something very different from what you intended. But I don't use Matlab much and I don't feel like reading the reference manual to figure out exactly what is going on, sorry!

6. Jun 19, 2014

D H

Staff Emeritus
You need a loop (or you need to use an advanced Matlab function that essentially does the loop for you).

You are testing whether X_input is less than -2, etc, and to be honest, I would not have given 7/8 for that code. You need to do this test on an element by element basis.

7. Jun 19, 2014

Maylis

Here is my new code that I get help with from a grad student instructor

Code (Text):
if ischar(X_input)
Y_output = upper(X_input);
elseif isnumeric(X_input)
for i = 1:length(X_input)
if X_input(i) < -2
Y_output(i) = -1./((X_input(i).^2) + 1);
elseif (X_input(i) >= -2) && (X_input(i) < -1)
Y_output(i) = sin(X_input(i));
elseif (X_input(i) >= -1) && (X_input(i) < 1)
Y_output(i) = X_input(i).^2;
elseif (X_input(i) >= 1) && (X_input(i) < 2)
Y_output(i) = 2;
elseif X_input(i) >= 2
Y_output(i) = 1./(X_input(i) + 1);
end
end
elseif islogical(X_input)
Y_output = logical(~X_input);
else
Y_output = NaN;
end
It didn't help that I had to write this code without learning with a loop is. But at least I was able to do the elseif part from AZ's help.