5.0

5.0 | 3 ratings Rate this file 36 Downloads (last 30 days) File Size: 5.6 KB File ID: #23294

ISMEMBERF

by

 

15 Mar 2009 (Updated )

Floating-point ISMEMBER (i.e., with round-off tolerance)

| Watch this File

File Information
Description

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

Acknowledgements

This file inspired Sound Transmission Class Stc and Impact Isolation Class.

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
18 Feb 2013 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.

24 Oct 2010 Bruno Luong

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

23 Oct 2010 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

22 Oct 2010 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)?

09 Mar 2010 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

09 Mar 2010 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

08 Mar 2010 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

13 Oct 2009 Carlos Adrian Vargas Aguilera

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

13 Oct 2009 Jan Simon

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.

10 Oct 2009 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.

09 Oct 2009 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().

Updates
16 Mar 2009

Extend ISMEMBERF to complex arrays

19 Mar 2009

Important change in the engine when using 'rows' option, which saves the runtime by about 30%. Some adjustment with automatic tolerance.

10 Oct 2009

Correct bug reported by David

13 Oct 2009

Correct bug reported by Jan, better description in H1 line (thanks)

23 Oct 2010

Complete H1 lines with specification of LOC and membership criteria

24 Oct 2010

Replace soon-deprecated strmatch with strncmpi

Contact us