Is the ismember function buggy?
Show older comments
Hell everybody,
I tried to use the ismember function to find out all values from a big matrix A (700k x 1 entries) that are also part of a smaller matrix B (70k x 1).
Every element of B is also an element of A. Therefore I expect with
index = (find(ismember(A, B)));
That I get an array called index, with the same length as B and the entries are the position in B, right?
Now the case it, that this does not happen. length(index) << length(B).
Long story short: Some arbitrary values are not taken into account.
Any suggestions for a different approach that might work, or any idea how to bugfix the ismember function?
Answers (2)
Guillaume
on 23 Oct 2016
There is nothing wrong with ismember. You're testing how many elements of A are in B, not how many of B are found in A. I suspect that
all(ismember(B, A))
sum(ismember(B, A)) == numel(B)
will both return true.
Note that there is usually no reason to use find on the return value of ismember. You can use the returned logical array directly for indexing.
John D'Errico
on 23 Oct 2016
My gut tells me that you are falling under the wheels of floating point arithmetic.
ismember([0.2 0.3],0:.1:1)
ans =
1 0
This is NOT a bug!!!!!! This is NOT a bug!!!!!!!
It is a simple fact of life in the wacky wonderful world of floating point numbers. I conjecture that you THINK that all of your numbers are in both arrays, but in fact, they are not exactly so. In fact 0.3 is not even exactly 0.3, because most decimal numbers are not exactly representable in the IEEE binary storage format used in almost all computer languages.
sprintf('%55.55f',0.3)
ans =
0.2999999999999999888977697537484345957636833190917968750
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!