5.0

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

ISMEMBERF

by Bruno Luong

 

15 Mar 2009 (Updated 24 Oct 2010)

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

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (10)
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().

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.

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.

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

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

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

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

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

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

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

Please login to add a comment or rating.
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

Tag Activity for this File
Tag Applied By Date/Time
ismember Bruno Luong 16 Mar 2009 09:26:16
float Bruno Luong 16 Mar 2009 09:26:16
round off Bruno Luong 16 Mar 2009 09:26:16
numerical errors Bruno Luong 16 Mar 2009 09:26:16

Contact us at files@mathworks.com