5.0

5.0 | 1 rating Rate this file 127 downloads (last 30 days) File Size: 5.46 KB File ID: #23294

ISMEMBERF

by Bruno Luong

 

15 Mar 2009 (Updated 13 Oct 2009)

Code covered by BSD License  

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

Download Now | 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)
Zip File Content  
Other Files ismemberf.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
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

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)

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
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com