How can I check if two variables return the same value?

I defined two variables which are supposed to return the same numerical value through two different formulas.
I want to make sure that the two variables indeed return the same value. Let's call the two variables A and B.
If in the Editor I input
A
B
the values returned are exactly the same. However, if I input
A == B
I visualize
ans =
logical
0
while I expect "1".
What am I doing wrong? If I input
diff([A, B]) I am returned zero.
Thanks.

2 件のコメント

Stephen23
Stephen23 2022 年 2 月 21 日
"the values returned are exactly the same"
Most likely the values are not exactly the same, or contain NaNs. But because you did not give us the values, we cannot check this. Please upload both values in one mat file by clicking the paperclip button.
Torsten
Torsten 2022 年 2 月 21 日
Did you choose
format long
to display the numbers ?
There might be a difference between them due to floating point arithmetic.

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

回答 (1 件)

KSSV
KSSV 2022 年 2 月 21 日
編集済み: KSSV 2022 年 2 月 21 日

0 投票

If A and B are floatinng point numbers (which are mostly), then you cannot use A == B. In this case to comapre them fix a tolerance value and check for the absolute difference.
tol = 10^-5 ;
abs(A-B)<tol

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

リリース

R2021a

タグ

質問済み:

AG
2022 年 2 月 21 日

コメント済み:

2022 年 2 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by