1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jun 19, 2014 #1

    Maylis

    User Avatar
    Gold Member

    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:

  2. jcsd
  3. Jun 19, 2014 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  4. Jun 19, 2014 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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].
     
  5. Jun 19, 2014 #4

    Maylis

    User Avatar
    Gold Member

    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
  6. Jun 19, 2014 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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!
     
  7. Jun 19, 2014 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  8. Jun 19, 2014 #7

    Maylis

    User Avatar
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Conditional operator if-else-elseif-end with switch-case combined
  1. End fixity conditions (Replies: 3)

  2. Conditional Operators (Replies: 7)

Loading...