필터 지우기
필터 지우기

MATLAB equivalent to IFTE function

조회 수: 1 (최근 30일)
Irl Smith
Irl Smith 2020년 11월 19일
답변: Irl Smith 2020년 11월 20일
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
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

댓글을 달려면 로그인하십시오.

채택된 답변

Irl Smith
Irl Smith 2020년 11월 20일
Thanks to the community for several interesting answers. I failed to clearly define IFTE in my question; in some computer languages, the construct "If Something, Then Do_A, Else Do_B" can be written as a single function call IFTE(Something,Do_A,Do_B). This is clearly a flow-control statement and wouldn't really make sense as an anonymous function, but the version I really wanted, which is more like "X = IFTE(Condition_on_X,Do_A_to_X,Do_B_to_X)", seems to fit into the mold of an anonymous function. The answers supplied were more involved than I was hoping; I thought I just couldn't find the equivalent in the Help. Apparently my search of Help was OK, it ain't there.
So, thanks again to the people who commented, and I'll take the answer to be, "There ain't no such animal".

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by