Conditional operator if-else-elseif-end with switch-case combined

AI Thread Summary
The discussion focuses on correcting a MATLAB code that uses conditional operators and switch-case statements for a piecewise function. The original code incorrectly applies a switch statement, leading to issues with output when testing various input types. It is suggested that the user should replace the switch-case structure with nested if-elseif-else statements to handle each case properly. The importance of element-wise testing for numeric inputs is emphasized, along with the need for a loop to iterate through array elements. The revised code provided demonstrates the correct implementation of these concepts.
gfd43tg
Gold Member
Messages
947
Reaction score
48

Homework Statement


#1 in the attachment


Homework Equations





The Attempt at a Solution


My code is working for all of the numeric, logical, character portions. I got 7/8 points
Code:
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?
 

Attachments

Physics news on Phys.org
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.
 
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].
 
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:
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!
 
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.
 
Here is my new code that I get help with from a grad student instructor

Code:
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.
 

Similar threads

Replies
3
Views
3K
Replies
2
Views
3K
Replies
2
Views
10K
Replies
14
Views
5K
Back
Top