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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by