Issues with function ismember

조회 수: 8 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by