三元运算在八度中有效吗?

Roc*_*tal 5 matlab octave conditional-operator

问题是三元运算有效,因为我无法在网上找到任何与之相关的文档。而且我还发现三元在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)

输出:

GNU Octave, version 6.1.0
Copyright (C) 2020 The Octave Project Developers.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  For details, type 'warranty'.

Octave was configured for "x86_64-w64-mingw32".

Additional information about Octave is available at https://www.octave.org.

Please contribute if you find this software useful.
For more information, visit https://www.octave.org/get-involved.html

Read https://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.

>> income = 60000;
>> compute_taxes
taxes = 7000
>> income = 50000;
>> compute_taxes
taxes = 5000
>>
Run Code Online (Sandbox Code Playgroud)

Tas*_*nou 5

任何语言中的三元运算符的任何定义与 if语句之间都有一些重要的区别。

主要的一点是后者是一个语句,而三元运算符根据定义是一个表达式。换句话说,您期望它返回一个值。Octave/Matlab 中的 if 块不会“返回”值(即您不能这样做a = if ... endif)。如果您需要“返回”某些内容,则需要将其分配给循环内的外部变量,然后在循环外使用该变量。

第二件大事是三元运算符应该是可链接的。也就是说,您应该能够执行“如果这个,那么那个,否则如果那个,那么其他的事情”,这样在一长串比较的末尾,您返回一个值。


现在octave和matlab都没有这种运算符。但在八度音程中,很容易模拟一个,例如使用单元格:

M1 = false; M2 = false; M3 = true;
{'expression1', 'expression2', 'expression3', 'fallback'}([ M1, M2, M3, true ]){1}
Run Code Online (Sandbox Code Playgroud)

如果你真的愿意,你可以将这种东西变成一个很好的匿名函数,它返回其掩码/测试为真的第一个表达式:

tern = @(varargin) reshape(varargin,2,[])(2,:)([reshape(varargin,2,[]){1,:}]){1}
tern( M1, 1, M2, 2, M3, 3, true, 4 )   % ans = 3
Run Code Online (Sandbox Code Playgroud)

其中“后备”只需在“后备”值之前进行显式评估为“true”的测试即可实现。

注意:这种在匿名函数内“链接”两个或多个索引操作的方式仅适用于八度音程,不幸的是,matlab 不允许链接操作,例如a(1)(2)。不过,没有什么可以阻止您创建一个等效的“正确”外部函数,您可以在第二次索引之前将中间结果保存到临时变量中;因此,从概念上讲,该策略也适用于 MATLAB。


或者,您可以使用稍微不同的语法制作一个更简单的“三元运算符”(好吧,函数),其中分别传递“测试/掩码”数组、“表达式”和“后备”单元格。例如

tern2 = @(M,E,F) [E, F]{find([M, true], 1)}
tern2( [M1, M2, M3], {1, 2, 3}, {4} )   % ans = 3
Run Code Online (Sandbox Code Playgroud)

最后,还有一个ifelse函数,它在精神上类似, ie ifelse(test, expr_if_true, expr_if_false),但这并不是真正的三元运算符/函数,因为它不可链接;它对于基于“真/假掩码”在两个替代方案之间进行选择更有用,例如

ifelse( [M1, M2, M3], {1, 2, 3}, {4,5,6} )
% ans = { 4, 5, 3 }
Run Code Online (Sandbox Code Playgroud)