and if in matlab?

조회 수: 3 (최근 30일)
darksideofthemoon101
darksideofthemoon101 2011년 4월 8일
Hi,
I want to run an if loop with 2 conditions, but I can't see an "and-if" equivalent.
Eg:
if 0<x<1
*and y2<y1*
z=y2;
else
end
What operator can I use instead of and?
Thanks,
Richard
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 12월 8일
There is no such thing as an if loop.

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

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 4월 8일
&
&&
and(x,y)
doc and
  댓글 수: 1
darksideofthemoon101
darksideofthemoon101 2011년 4월 8일
Thanks Sean de, I just had a total blindspot there for some reason!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 4월 8일
0<x<1 does not do what you want it to!! It is parsed as
(0<x)<1 . 0<x returns a boolean value, 0 or 1, and then that value is tested according to whether it is < 1 which is only the case if the operation was false. The code is thus equivalent to
~(0<x)
and so is the same as
isnan(x) || 0 >= x
I doubt that you would want 0<x<1 to be true when x is nan !!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by