问题是三元运算有效,因为我无法在网上找到任何与之相关的文档。而且我还发现三元在MATLAB 中是不可能的,因此任何建议和答案都将在此处受到赞赏。
#带三元运算的代码
taxes = (income > 50000)*(((income-50000) * 0.20)+(0.10*50000)) + (~(income > 50000))*0.10 *50000
#Condition True and this computation False then this computation
Run Code Online (Sandbox Code Playgroud)
#代码与if和else
if (income) > 50000
#income = income - 50000
#Taxed at 10% (i.e 0.10) for $ 50000
#taxes = 50000 * 0.10
#Rest income will be Taxed at 20% (i.e 0.20)
taxes = 50000 * 0.10 + ((income-50000) * 0.20)
else
#Taxed at 10% (i.e 0.10)
taxes = income * 0.10
endif …
Run Code Online (Sandbox Code Playgroud)