0

If we do the following,

function [z] = a(x,y)
syms x y
If I do the following code,
if x>5
    z=x+y
else
    z=x-y
end
end

then I hope to have something like (as in Mathematica)

z=IF[x>5,x+y,x-y]

I do not insist on having the exact same code as above. I am flexible. I just want to have "just one line" which expresses how input of function results in output when there are if in the code.

Is this possible?

user42459
  • 883
  • 3
  • 12
  • 29

1 Answers1

1

Matlab does not support tertiary operator i.e. a = b ? c : d. However there is a hacky way of getting the same result.

z = (x>5)*(x+y) + (x<=5)*(x-y)
Harjatin
  • 1,313
  • 1
  • 12
  • 22