writing "if" command for a>f>c

조회 수: 7 (최근 30일)
mohammad
mohammad 2011년 8월 30일
how can write if command for < and > together. for example if 1.01>a>0.9 then do cprintf('g','ok') else do cprintf('r','not ok'). when i write this like this: if 1.01>a>0.9 cprintf('g','ok'); else cprintf('r','not ok');
matlab only considers 1.01>a
thanks alot

채택된 답변

Paulo Silva
Paulo Silva 2011년 8월 30일
There's no cprintf on MATLAB, use disp to show simple messages on the command line or fprintf for extra features.
a=1;
if 1.01>a && a>0.9
disp('ok')
else
disp('not ok')
end
  댓글 수: 5
Paulo Silva
Paulo Silva 2011년 8월 30일
or(a>5, a<3) %first way to do the or
a>5 || a<3 %second way to do the or
Both ways do the same thing
doc or %see the documentation of the or function, same goes for the and function
mohammad
mohammad 2011년 8월 31일
really thanks Paulo

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

추가 답변 (1개)

geomod
geomod 2011년 8월 30일
I cannot say why but, if you write:
if 0.9<a<1.01
OR
if 1.01>a && a>0.9
then it should run...
  댓글 수: 3
mohammad
mohammad 2011년 8월 30일
thanks 4 reply
if 0.9<a<1.01 not work
but 1.01>a && a>0.9 works
if there is no way to work in form like 0.9<a<1.01 , i use && form?
Walter Roberson
Walter Roberson 2011년 8월 31일
if 0.9<a<1.01
means
if (0.9<a)<1.01
The part in () will evaluate to a logical value, either false (numeric 0) or true (numeric 1). Both 0 and 1 are < 1.01 so the overall expression would always evaluate as true.
MATLAB has no three-part logical operators at all, no built in way to test ranges.
If you wanted to really mess with the mind of the person reading the code, you could use:
if ~isempty(0.9:a-0.9:1.01)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by