if else loop

I have a variable "v" in my code which after calculation gets value either 1 or 1.0000 And 1 is not equal to 1.0000 here. I want to apply a if else if loop as follows : if v==1 statements elseif v==1.0000 statements else statements end
But this is not working. Please tell me the correct way to do it. Matlab isn't executing "v==1.0000". Why??

1 件のコメント

Walter Roberson
Walter Roberson 2012 年 4 月 9 日
There is no such thing as an "if loop". "if" does not loop: it executes once. "if" might be coded within a loop, but it is not a loop.

サインインしてコメントする。

回答 (2 件)

Daniel Shub
Daniel Shub 2012 年 4 月 9 日

0 投票

Try
format long
and then look at your v. You can also look at
v-1
What you will see is that when MATLAB says v is equal to 1.0000, what MATLAB is telling you is that v is not equal to 1, but rather something slight more than 1.
There is a huge discussion here (and else where) on floating point comparisons.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2012 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by