Ternary: Inline if/else and switch/case
An inline conditional. Can function as an if/else of as a switch/case.
Usage:
If/else behavior:
tern(cond,a,b) returns: a if cond is true, else returns b
Switch/case behavior (>3 arguments):
tern(s,case1,value1,..,caseN,valueN,<defaultValue>), same as:
switch s
case case1
return value1
..
case caseN:
return valueN
otherwise:
return defaultValue (default = NaN)
Examples:
If/else behavior:
disp(tern(val>0,'value is positive!', 'value is negative'));
Switch/case behavior:
disp(tern(val,1,'value is one!', 2, 'value is two!', 'values is not 1 or 2'));
Citar como
David (2026). Ternary: Inline if/else and switch/case (https://la.mathworks.com/matlabcentral/fileexchange/81863-ternary-inline-if-else-and-switch-case), MATLAB Central File Exchange. Recuperado .
Compatibilidad con la versión de MATLAB
Compatibilidad con las plataformas
Windows macOS LinuxEtiquetas
Descubra Live Editor
Cree scripts con código, salida y texto formateado en un documento ejecutable.
| Versión | Publicado | Notas de la versión | |
|---|---|---|---|
| 1.0.0 |
