Matlab ISMEMBER uses strict exact comparison between floats.
The ISMEMBERF (note the "F") can tolerate some round off error.
It is possible to configure the tolerance.'rows' option is also supported.
Example:
[tf, loc]=ismember(0.3, 0:0.1:1) % returns false
[tf, loc]=ismemberf(0.3, 0:0.1:1) % return true
Bruno Luong (2021). ISMEMBERF (https://www.mathworks.com/matlabcentral/fileexchange/23294-ismemberf), MATLAB Central File Exchange. Retrieved .
Inspired: Sound Transmission Class STC, Impact Isolation Class
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Bruno, I can't thank you enough. I have been scratching my head for days trying to figure out why ismemeber was not working and then trying to rectify it, until I came across this!.
Great! (I like the name of the function as well.)
Small point: I was expecting the syntax
ismember(A,B,tol)
to work.
At least give an error/warning that the 3rd input was not recognized/set.
Correction, it returns the index loc of the largest S(loc) such that
S(loc)-tol <= A < S(loc)+tol
When calling [b loc] = ismemberf(A, S), I confirm that the loc returned is the highest index such that:
S(loc)-tol <= A < S(loc)+tol
This is a great addition. I'm surprised this isn't part of the core Matlab. A useful note to users is that whereas the ismember documentation specifies that loc is an array "containing the highest index in S for each element in A that is a member of S", ismemberf does not guarantee the highest index. That might be a nice feature to add. Or is there a different index that can be guaranteed (e.g., the lowest)?
Well, I suggest at this point to document this behaviour and to add in the see also line the HISTC.
Something like:
A(i) is member of the set S if the following condition is verified at least once:
k - tol <= A(i) < k + tol for k = S(1)...S(K)
Best
Hi Oleg,
This behavior is known (but not documented) due to the use of HISTC as you have correctly pointed out. User should never rely on the threshold tolerance in order to discriminate floats through exact comparison. Please enlarge the tolerance or add point to set member to work around.
Bruno
Dear Bruno,
I want to bring this case to your attention:
tf = ismemberf(2.8, 2.9, 'tol',.1) --> 1
tf = ismemberf(3, 2.9, 'tol',.1) --> 0
It's following histc behaviour on line 287 (lowB <= x < uppB), is this meant?
Regards
Oleg
This is a nice function, thanks bruno for sharing it. You all may check out my NUMCMP function too.
http://www.mathworks.com/matlabcentral/fileexchange/21190-numcmp-m
Cheers, Carlos
Beside the fact, that this is of practical use and works as expected, it helps to answer dozens of questions in the newsgroup: why does ANY((0:0.1:1) == 0.3) reply FALSE???
The help is complete and descriptive, contains author and date, exact and really useful comments. The H1-line is not helpful.
Minor bug in line 125: if ndims(A) ~= 2 || ndims(A) ~= 2. A simple [tf, loc] output might be more efficient than the VARARGOUT/Cell/DEAL/MIN/MAX/NARGOUT constructions.
Thank you David for pointing out the bug. I better workaround IMO is change the line #257 to:
dS = realmin(class(S));
while the new version of ISMEMBERF is submitted.
PROBLEM: ismemberf([0 1 2], 0) will return [0 0 0]. It seems this is because line 263 will auto-configure tol to zero which causes unexpected results in ihi (line 274) that causes tf to be set incorrectly in line 276.
WORK-AROUND: This behavior can be avoided if 'tol' is explicitly set when calling ismemberf().