Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

matlab - Is Ternary operation valid in Octave?

Question is ternary operation valid as i am not able to find any document related to it online. And i also find out that ternary is not possible in MATLAB so any suggestion and answers will be appreciated here.

#Code with ternary operation

taxes = (income > 50000)*(((income-50000) * 0.20)+(0.10*50000)) + (~(income > 50000))*0.10 *50000
#Condition True and this computation False then this computation

#Code with if and 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

Output:

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
>>
question from:https://stackoverflow.com/questions/66055370/is-ternary-operation-valid-in-octave

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are a few important differences between any definition of a ternary operator in any language, and an if statement.

The main one is that the latter is a statement, whereas a ternary operator is by definition an expression. In other words, you expect it to return a value. An if block in octave/matlab does not "return" a value (i.e. you can't do a = if ... endif). If you need something 'returned', you need to assign it to an external variable inside the loop, and then use that variable outside the loop.

The second big thing, is that a ternary operator should be chainable. Ie you should be able to do "if this, then that, otherwise if that then that other thing", such that at the end of a long chain of comparisons you return a single value.


Now, octave and matlab do not have this kind of operator. But in octave, it's easy enough to simulate one, e.g. with cells:

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

If you really want to, you can make that kind of thing into a nice anonymous function, which returns the first expression for which its mask/test was true:

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

where 'fallback' is achieved simply by having a test that evaluates to 'true' explicitly before the 'fallback' value.

Note: This style of 'chaining' two or more indexing operations inside an anonymous function only works in octave, as unfortunately matlab does not allow chained operations, e.g. a(1)(2). There is nothing stopping you from making an equivalent 'proper', external function though, where you save intermediate results to a temporary variable before indexing it a second time; therefore, conceptually, this strategy will also work in matlab.


Alternatively, you could make a simpler 'ternary operator' (well, function) using a slightly different syntax, where you pass a 'tests/masks' array, and 'expressions', and 'fallback' cells separately. E.g.

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

Finally, there is also the ifelse function, which is similar in spirit, i.e. ifelse(test, expr_if_true, expr_if_false), but this is not really a true ternary operator/function, since it is not chainable; it's more useful for choosing between two alternatives based on a 'true/false mask', e.g.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...