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

In summary, the student's code is working for all of the numeric, logical, and character portions. However, they are having problems with the elseif part of the code. They need to do the test on an element by element basis.
  • #1
gfd43tg
Gold Member
950
50

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

  • CDLassignment.pdf
    94.8 KB · Views: 372
Physics news on Phys.org
  • #2
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
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
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:
  • #5
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
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
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.
 

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

1. How does the conditional operator if-else work?

The conditional operator if-else is a decision-making statement in programming that allows for two different paths of execution based on a specific condition. If the condition is true, the code inside the if statement will be executed. If the condition is false, the code inside the else statement will be executed.

2. What is the difference between "if-else" and "if-elseif-else" statements?

The "if-elseif-else" statement allows for multiple conditions to be checked and multiple code blocks to be executed. If the first condition is true, the code inside the "if" block will be executed and the rest of the statement will be skipped. If the first condition is false, the next condition will be checked and its corresponding code block will be executed. This continues until a condition is found to be true or until the final "else" block is reached.

3. How does the switch-case statement work in combination with if-else and elseif?

The switch-case statement allows for a value to be compared to multiple cases, and the corresponding code block for the matching case will be executed. If there is no match, the default code block will be executed. This can be useful in situations where there are multiple possible values to compare, rather than just a true or false condition.

4. Can the conditional operator if-else be nested within another if-else statement?

Yes, the conditional operator if-else can be nested within another if-else statement. This allows for even more complex decision-making in a program, as each if-else statement can have its own conditions and corresponding code blocks.

5. Is it necessary to use both if-else and switch-case in combination?

No, it is not necessary to use both if-else and switch-case in combination. Depending on the specific needs of a program, one may be more suitable than the other. It is important to understand the differences between the two and use them appropriately.

Similar threads

Replies
3
Views
2K
Replies
2
Views
2K
Replies
4
Views
3K
Replies
8
Views
2K
Replies
2
Views
10K
Replies
14
Views
4K
Back
Top