필터 지우기
필터 지우기

Need Conditional ElseIf statement

조회 수: 2 (최근 30일)
Sudharsan
Sudharsan 2012년 10월 12일
w=[200 205 188;169 255 156;192 168 172]
if(0<w(5)<255)
x=1;
else
x=2;
end
for this kind of code i'm getting x value as 1.can anyone help me to identify what mistake i have made...

채택된 답변

Doug Hull
Doug Hull 2012년 10월 12일
편집: Doug Hull 2012년 10월 12일
(0<w(5)<255)
You probably mean this:
((0 < w(5)) & (w(5) < 255))
Evaluating yours left to right:
0<w(5)<255
TRUE < 255
TRUE
  댓글 수: 1
Sudharsan
Sudharsan 2012년 10월 12일
Thank You Doug Hull :)

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

추가 답변 (1개)

Matt Kindig
Matt Kindig 2012년 10월 12일
Matlab won't correctly parse the 0<w(5)<255 statement as you intend. (Instead, it will evaluate (0 < w(5)) first (which returns a logical value 0 or 1). Matlab then evaluates (0 or 1) < 255, which is always true, so x=1 for all values of w.
Instead, use
if (0 < w(5)) && (w(5) < 255),
x= 1;
else
x=2;
end
  댓글 수: 1
Sudharsan
Sudharsan 2012년 10월 12일
Thank You Matt Kindig :)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by