ifelse and switch don't match
Show older comments
Hi, my problem is this code:
if A21 == N2
Re21 = N2r;
elseif A21 == I2
Re21 = I2r;
elseif A21 == R2
Re21 = R2r;
elseif A21 == T2
Re21 = T2r;
end
I2 and R2 have the same value so, when I put A21 = R2, the value of Re21 is I2r because MATLAB thinks that I2 and R2 are the same variable, It only took the first value in the code (I2). I need that MATLAB recognize that Re21 is R2r. How can I fix it? Same isue with Switch.
4 Comments
DGM
on 26 May 2022
MATLAB doesn't think I2 and R2 are the same variable. If A21 == R2, and R2 == I2, then A21 == I2. The second case matches, and that's it. The only assignment that is performed is Re21 = I2r.
if A21 == N2
Re21 = N2r;
elseif A21 == I2 % this is true
Re21 = I2r;
elseif A21 == R2 % this is also true, but it's never used if the prior case is true
Re21 = R2r;
elseif A21 == T2
Re21 = T2r;
end
That's how if-elseif conditionals work.
If R2r and I2r are unique values, then how do you propose to have a variable that is simultaneously equal to two different things? If instead they're identical values, then what's the difference?
Ignacio Brown Vidal
on 27 May 2022
Steven Lord
on 27 May 2022
Can you tell us (in words not code) what the various variable represent and what decision or thought process the code you've posted is supposed to implement? Having a better understanding of the underlying problem you're trying to solve may help us offer alternate solutions that may avoid the edge cases or potential problems of the solution you posted.
Ignacio Brown Vidal
on 27 May 2022
Edited: Ignacio Brown Vidal
on 27 May 2022
Accepted Answer
More Answers (0)
Categories
Find more on Numeric Solvers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!