50

is there a way of typing for if like:

var = (cond) ? true : false;

or do we have to use this format?

if (cond)
 true
else
 false
end
Dan
  • 45,079
  • 17
  • 88
  • 157
Gün Karagöz
  • 705
  • 1
  • 7
  • 12
  • But for a more general case, see some of the answers here: http://stackoverflow.com/questions/27561881/one-liner-for-if-true-then-a-else-b-in-matlab/27562100#27562100 – Dan Dec 24 '14 at 05:41

12 Answers12

39

MatLab doesn't have a ternary operator, or any other syntactic sugar for one-line if-statements. But if your if-statement is really simple, you could just write it in one line anyway:

if (cond); casetrue(); else; casefalse(); end

It's not as simple as ternary operator, but still better than writing it in 5 lines of code.

Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
  • 4
    Hoever simple, if/switch/loop statements aren't supported in lambda/anonymous function definitions in Matlab. – hyiltiz Feb 25 '20 at 15:20
20

Hmm... no one has mentioned this

fi = @(varargin)varargin{end-varargin{1}}

somewhere in the docs it is written the `end' is coming to one so this will be more future proof

fi = @(varargin)varargin{length(varargin)-varargin{1}}

Usage :

fi(input('Do you like Matlab ? '),'yes','no')
>> no

If your in the need of inline cases see Mathworks ...

Carel
  • 3,289
  • 2
  • 27
  • 44
  • 8
    This is probably the ugliest and hardest to understand piece of Matlab code I've seen. Usage is easy though, yay! – SimonH Aug 05 '22 at 14:05
  • Note that, unlike an `if`, both sides are evaluated. Also, this assumes that your first argument is logical (aka Boolean). Whereas an `if` will consider `3` to be `true`, this method will see `3` as `false`. You can simulate [how `if` works](https://mathworks.com/help/matlab/ref/if.html) using `fi = @(varargin) varargin{length(varargin) - logical(varargin{1})};`. If your input is a matrix, you can use `fi = @(varargin) varargin{length(varargin) - all(logical(varargin{1}), "all")};`. – FWDekker Nov 02 '22 at 15:44
13

If you only need true or false, you can do what MatlabSorter suggests. In case you want a real tertiary operator (i.e. a = b ? c : d), there is none in MATLAB. However, using the file supplied here, you can get close.

AVH
  • 11,349
  • 4
  • 34
  • 43
12

You can do

var = 5 > 4;

which will set var to true. Just substitute what ever you need for 5 > 4.

MatlabSorter
  • 1,290
  • 1
  • 11
  • 19
  • 8
    Na, you can use (a > b) as a factor and then add another product with the opposite statement : `var = (a < 0) * (-a) + (a >= 0) * (a)` – Karl Aug 03 '15 at 15:29
  • If we need to use the ternary operator inside disp or fprintf? – Naveen Feb 19 '16 at 21:28
10

MATLAB doesn't have conditional expressions, but in some situations you can get a similar effect by saying, e.g., var = cond*true_expr + (1-cond)*false_expr. Unlike C's conditional expression, this will of course always evaluate both true_expr and false_expr, and if cond happens not to be either 0 or 1 (note: false behaves like 0; true behaves like 1) you'll get crazy results.

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
  • 1
    It should be possible to create a function which takes a condition and two function handles. Then you have the wanted shortcut behaviour. – glglgl Dec 19 '14 at 08:29
  • 1
    Elegant but works only for numerical expressions, I tried it with strings and it returned them as an ASCII values array – BlackDog Dec 14 '16 at 08:49
  • This also does not work if `true_expr` or `false_expr` are `inf` – Eric May 16 '17 at 14:57
4

Quick and elegant. I would simply write tern( a>b, a, b) Downside is you have to copy paste everywhere, or have and extra file in the directory

function b = tern(cond, a, b)
    if cond
        b=a;
    end
end
  • 1
    beware that this does not work if you need lazy evaluation, e.g. `b` always evaluates regardless of `cond` (might seem obvious, but you probably just think abstract: 'Ah ternary operator here we go' and use it as normal, things might break for you)... – Jiří Mar 19 '22 at 13:13
4

The following solution has two major advantages over the solutions proposed so far:

  1. It supports lazy evaluation, i.e. only the selected input is evaluated.
  2. Unlike a if/else statement packed in one line, it can also be used in anonymous functions.
function out = tern(cond, resTrue, resFalse)

if cond
    res = resTrue;
else
    res = resFalse;
end 
out = res();  %<--- The trick!

The trick to achieve lazy evaluation is that the last line in tern evaluates the selected result. This way, you can pass anonymous functions to tern and only the selected will be invoked.

E.g. the following can be invoked without error, when the file with fileName is not existing:

x = tern( isfile( fileName), @()load( fileName ), ones(10 ));

It should be noted that, when res is not a anonymous function but of a different basic type (e.g. numeric, character, cell ) in the last line, res() evaluates to res itself.

So in summary, the only little difference to a real ternary operator is that it is required to add @() to the arguments that should only be conditionally evaluated.

user16372530
  • 692
  • 2
  • 13
3

Replace

c = (x ? a : b)

by

c = subsref({b; a}, substruct('{}', {x + 1}))

x should be a boolean value or 1 or 0.
true or 1 will select a
false or 0 will select b
This should work with all what cells can contain and can also be used in an intricate formular!

Juergen S.
  • 31
  • 1
3

@Leonid Beschastny is correct about the inlining the if-else-end statement, but If one must, then with any assignment value that can be evaluated as boolean, one can the shortcut boolean operators || and &&:

(cond) && ((var=true_val)||1) || (var=false_val);

Rules:

  1. shortcut boolean ops || and && must be used, NOT | or &
  2. will work with boolean, numeric and char assignments
  3. will NOT work with cell array or function handles
  4. the assignments must be wrapped in parenthesis.
  5. (var=true_val) must be followed by ||1 in case true_val == false
  6. true and false assignments may be to different variables (i.e. (var1=true_val) while (var2=false_val))
  7. true_val and false_val can be different types provided each then can be evaluated as boolean

Alternatively one can do this:

cond && (func1(..)||1) || func2(...);

provided func1 and func2 return a boolean testable value including nothing at all (but no cell arrays!):

codechimp
  • 1,509
  • 1
  • 14
  • 21
  • For which Matlab version is this? I have R2018a and the assignment var=true_val does not work. Matlab reports a syntax error. – tommsch Jun 25 '20 at 06:39
2

I use this style frequently:

cond = what < ever;

n = getfield([23,42], {1+(what < ever)}) % for any 1x1-data
s = cell2mat(getfield({'no','yes'}, {1+(what < ever)})) % for nonuniform

it's compact enough to not require a helper function

rascr
  • 21
  • 1
1

I know this is late, but for people trying to find an answer I came up with this.

If you want a boolean var = (cond) ? true : false; you can just do:

var = cond;

If you want a number var = (cond) ? value_if_true: value_if_false; then do:

var = cond*value_if_true + ~cond*value_if_false;

This is all using that matlab treats true as 1 and false as 0. Therefore that's why the first statement expanded really is

var = cond*1+ ~cond*0;

If you want a text var = (cond) ? text_if_true: text_if_false; then do:

[repmat(text_if_true,1,cond) repmat(text_if_false,1,~cond)];

Unfortunately you can't mix numerical values with text using this method, just write it with an if-else-end statement.

Tower
  • 11
  • 2
0

I found this question when I needed to set my matrix values to 0 or 1 based on an arbitrary condition (e.g. element value is greater than pi). Turns out it's as easy as:

M > pi

Sure it's less generic than the original question but this corner case may help someone.

Kolyunya
  • 5,973
  • 7
  • 46
  • 81