MATLAB Answers

Kelly
0

Issues with function ismember

Asked by Kelly
on 2 Mar 2019
Latest activity Commented on by Kelly
on 2 Mar 2019
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 Comment

Remove find ,swap A and B

Sign in to comment.

2 Answers

Answer by Jos (10584)
on 2 Mar 2019
 Accepted Answer

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 Comment

Thank you so much, it worked!

Sign in to comment.


Answer by Stephan
on 2 Mar 2019
Edited by Stephan
on 2 Mar 2019

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 Comment

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.

Sign in to comment.