Is there a Matlab conditional IF operator that can be placed INLINE like VBA's IIF

Lance Roberts picture Lance Roberts · Jan 30, 2013 · Viewed 36.2k times · Source

In VBA I can do the following:

A = B + IIF(C>0, C, 0)

so that if C>0 I get A=B+C and C<=0 I get A=B

Is there an operator or function that will let me do these conditionals inline in MATLAB code?

Answer

Alex picture Alex · Dec 23, 2013

How about simply using the fact that MATLAB automatically converts variable types when required by the operation? E.g., logical to double.

If your variables are scalar double, your code, I believe, can be replaced by

a = b + (c > 0) * c;

In this case, the operator (c > 0) values 1 (logical type) whenever c > 0 and values to 0 otherwise.