MATLAB equivalent to IFTE function
1 回表示 (過去 30 日間)
古いコメントを表示
Is there a function equivalent to the IFTE found in some languages? Let A be a vector of doubles, e.g. [1 2 3 4 5 6 7 8]. I want to even values by 10 and odd values by 20. Using the notional IFTE, I could write
Result = IFTE(mod(A,2)==0,10,20)
One way to do this in MATLAB is
AIsEven = mod(A,2)==0; A(AIsEven) = 10; A(~AIsEven) = 20
In this toy example the extra statements are not a problem, but I would like to package this as an anonymous function:
FindEvens = @(x)(IFTE(mod(x,2)==0),10,20)
1 件のコメント
jessupj
2020 年 11 月 19 日
編集済み: jessupj
2020 年 11 月 19 日
can you clarify what IFTE is?
if your question is really something like: "how can i implement a conditional statement as an anonymous function?", see W.Robertson's answer to https://www.mathworks.com/matlabcentral/answers/50195-is-it-possible-to-write-several-statements-into-an-anonymous-function
採用された回答
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!