Issues with function ismember

10 ビュー (過去 30 日間)
Kelly
Kelly 2019 年 3 月 2 日
コメント済み: Manas Shrivastava 2020 年 12 月 3 日
Hello,
Data: Matrix A: 3462x1 double, Matrix B: 33x1 double
I am trying to find and get the index of my data (A) which is also found in my second matrix (B). However it returns with values of zero, eventhough I know that all the values in A is found within matrix B.
I am writing...
[ind val] = find(ismember(A,B));
However the answer is gives me this [ ] for both ind and val.
I am hoping for 'ind' to index where the values in matrix B are located within matrix A. And I am hoping for 'val' to result in mixture of 1 and 0's stating whether this statement is true (1) or false (0).
Can someone suggest what it is i am doing wrong?
Cheers :)
  1 件のコメント
madhan ravi
madhan ravi 2019 年 3 月 2 日
編集済み: madhan ravi 2019 年 3 月 2 日
Remove find ,swap A and B

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

採用された回答

Jos (10584)
Jos (10584) 2019 年 3 月 2 日
Comparing floating point numbers is always tricky! Two number may look the same for you, but could still be slightly different for a computer! Hence, the empty result.
You may want to use a tolerance, using ISMEMBERTOL, for example:
tf = ismembertol(B, A, 1e-3)
min(abs(A - B')) % there are indeed small differences
  2 件のコメント
Kelly
Kelly 2019 年 3 月 2 日
Thank you so much, it worked!
Manas Shrivastava
Manas Shrivastava 2020 年 12 月 3 日
Thanks!

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

その他の回答 (1 件)

Stephan
Stephan 2019 年 3 月 2 日
編集済み: Stephan 2019 年 3 月 2 日
Hi,
ismember(A,B)
returns a logical 1 at the Locations where an element of B occurs in A. Therefore it is normal that zeros occour in the result in general.
If you call find with two output elements like you do, the reusult is row and column number - not value. To get the values use:
vals = A(ismember(A,B))
If the results are empty there is no occurence from B-values in A. If you expect them to be in it check B and A. Maybe this is due to rounding errors or other issues. For this we would need to know more details.
Best regards
Stephan
  1 件のコメント
Kelly
Kelly 2019 年 3 月 2 日
Thank you for responding :)
I have just tried this but the answer it gives me is still
vals = [ ]
I have attached the data I am using.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by