Issues with function ismember

23 次查看(过去 30 天)
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 :)

采纳的回答

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

更多回答(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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by