vector equals to value in matrix

i have a vector, A=0.9997 and a matrix gg=(0.35:0.0001:2); when i try to run,= any(eq(A,gg)) or any(A==gg); to find if A is in gg it always returns it as zero even though it return one as it is part of the elements of gg .if i run any(eq(0.9997,gg)), it returns it as one. Is there any reason for this and how can i rectify this?

回答 (2 件)

M
M 2018 年 7 月 9 日

0 投票

The three following expressions:
any(eq(A,gg))
any(A==gg)
any(eq(0.9997,gg))
are the same and equal to 1. What is your problem exactly?

4 件のコメント

Nkenna Amadi
Nkenna Amadi 2018 年 7 月 9 日
yes but when i run the first two it returns ans = 0
madhan ravi
madhan ravi 2018 年 7 月 9 日
編集済み: madhan ravi 2018 年 7 月 9 日
When I used the first two codes , my error message was matrix dimensions must agree, I think only it recognises numbers?
Nkenna Amadi
Nkenna Amadi 2018 年 7 月 9 日
thats my problem because i have a the value of A changes as it is a loop, so i cant just put a number
M
M 2018 年 7 月 9 日
You didn't say that the value of A changes in a loop. Look at Dennis' answer for more details.

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

Dennis
Dennis 2018 年 7 月 9 日
編集済み: Dennis 2018 年 7 月 9 日

0 投票

A=0.9997 and changing A in a loop are 2 different things.
This code will return 1.
gg=(0.35:0.0001:2);
A=0.9997;
any(A==gg)
This code wont, though A is 0.9997.
gg=(0.35:0.0001:2);
A=1.0997-0.1
any(A==gg)
The reason for this is that floating point numbers are not represented 100% accurate in binary form.
You can use abs(a-b)<tolerance to avoid this.
gg=(0.35:0.0001:2);
A=1.0997-0.1
any(abs((A-gg))<1e-10)
For more information on the topic please have a look here

カテゴリ

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

タグ

質問済み:

2018 年 7 月 9 日

コメント済み:

M
M
2018 年 7 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by