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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no ternary operator in Matlab. You can, of course, write a function that would do it. For example, the following function works as
iifwith n-d input for the condition, and with numbers and cells for the outcomesaandb:For a more advanced solution, there’s a way to create an inline function that can even do elseif, as outlined in this blog post about anonymous function shenanigans:
You use this function as
where you replace the dots with any number of additional condition/value pairs.
The way this works is that it picks among the values the first one whose condition is true.
2*find(),1,'first')provides the index into the value arguments.