File Exchange

image thumbnail

ISMEMBERF

version 1.10.0.0 (5.6 KB) by Bruno Luong
Floating-point ISMEMBER (i.e., with round-off tolerance)

4 Downloads

Updated 24 Oct 2010

View Version History

View License

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

Cite As

Bruno Luong (2021). ISMEMBERF (https://www.mathworks.com/matlabcentral/fileexchange/23294-ismemberf), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (16)

Fredrik Johansson

Nibodh Boddupalli

Satyajit Jadeja

Luke Eley

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!.

Nitin

Thierry Dalon

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.

Bruno Luong

Correction, it returns the index loc of the largest S(loc) such that
S(loc)-tol <= A < S(loc)+tol

Bruno Luong

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

Eric Patterson

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)?

Oleg Komarov

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

Bruno Luong

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

Oleg Komarov

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

Jan

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.

Bruno Luong

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.

David

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().

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!