Issues with function ismember

15 visualizzazioni (ultimi 30 giorni)
Kelly
Kelly il 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 Commento
madhan ravi
madhan ravi il 2 Mar 2019
Modificato: madhan ravi il 2 Mar 2019
Remove find ,swap A and B

Accedi per commentare.

Risposta accettata

Jos (10584)
Jos (10584) il 2 Mar 2019
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 Commenti
Kelly
Kelly il 2 Mar 2019
Thank you so much, it worked!

Accedi per commentare.

Più risposte (1)

Stephan
Stephan il 2 Mar 2019
Modificato: Stephan il 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 Commento
Kelly
Kelly il 2 Mar 2019
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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by