ifelse and switch don't match
Mostrar comentarios más antiguos
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 comentarios
DGM
el 26 de Mayo de 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
el 27 de Mayo de 2022
Steven Lord
el 27 de Mayo de 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
el 27 de Mayo de 2022
Editada: Ignacio Brown Vidal
el 27 de Mayo de 2022
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Numeric Solvers en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!