A query relating a function with 3 input arguments.

조회 수: 1 (최근 30일)
Alek Poudel
Alek Poudel 2021년 8월 12일
댓글: Alek Poudel 2021년 8월 12일
My code is:
function i = Three_inputsFunction (x,y,z)
if x>y>z || y>x>z
x+y
elseif x<y<z || x<z<y
y+z
else
x+z
end
end
>> Three_inputsFunction (-18,-3,-6)
ans =
-21
Query : Why is the answer '-21' and not '-9'?

채택된 답변

Stephen23
Stephen23 2021년 8월 12일
편집: Stephen23 2021년 8월 12일
"Why is the answer '-21' and not '-9'?"
Because this code
x>y>z
(x>y)>z
which (because true==1 and false==0) is equivalent to either of these
1>z
0>z
You need this instead:
x>y && y>z
Also note that you do not define the function output i.
  댓글 수: 6
Stephen23
Stephen23 2021년 8월 12일
@Alek Poudel: you can also show your thanks by accepting my answer :)
Alek Poudel
Alek Poudel 2021년 8월 12일
ohh yeah, done! didn't know about that before. Cheers!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by