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

Click For Summary

Discussion Overview

The discussion revolves around the implementation of conditional operators in MATLAB, specifically focusing on the use of if-else and switch-case structures in a piecewise function. Participants are addressing a homework problem that involves processing different types of input (character, numeric, logical) and generating corresponding outputs based on specified conditions.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes their initial code and expresses uncertainty about the use of NaN in the else clause, questioning how to test for other data types.
  • Another participant clarifies that NaN is a numerical value and suggests that the switch statement is not appropriate for the intended logic, recommending a series of if-elseif statements instead.
  • A different participant points out that the assignment requires element-wise processing of the input array, which the original code does not achieve.
  • One participant inquires whether the switch-case structure should be avoided entirely and seeks guidance on structuring the if-elseif-else block for the piecewise function.
  • Another participant confirms that nesting if-elseif structures is a valid approach and expresses surprise that the original code compiles correctly, indicating potential misunderstandings in its logic.
  • A participant suggests that a loop is necessary for element-wise evaluation of the input, criticizing the original approach for not handling this correctly.
  • A later post shares revised code that incorporates a loop and multiple if-elseif conditions, acknowledging assistance received from a graduate student instructor.

Areas of Agreement / Disagreement

Participants generally agree that the original use of the switch-case structure is not suitable for the problem at hand and that element-wise processing is necessary. However, there is no consensus on the best approach to implement the solution, as various suggestions are offered without a definitive resolution.

Contextual Notes

Limitations include the initial misunderstanding of how to handle different data types and the need for element-wise operations, which were not addressed in the original code. The discussion highlights the complexity of using conditional structures in MATLAB for this specific homework problem.

gfd43tg
Gold Member
Messages
949
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 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 14 ·
Replies
14
Views
5K