Be the first to rate this file! 126 downloads (last 30 days) File Size: 2.57 KB File ID: #23035

lexcmp

by Ben Petschel

 

19 Feb 2009 (Updated 14 Sep 2009)

Code covered by BSD License  

Fast C-style array/string comparison (by lexical ordering)

Download Now | Watch this File

File Information
Description

lexcmp(s1,s2) returns:
 -1, if s1<s2
  0, if s1=s2
  1, if s1>s2
where the comparison is made on the first elements that differ. s1 and s2 can be strings, numeric arrays or other compatible objects (see the help for more details).

Although this has already been done a few times before, this implementation is among the most efficient and the core routine is in a form that easily translates to mex/C (something similar as a built-in function would be nice to have).

The core function is similar to S. Helsen's strcmpc.m with some performance tweaks. The function is vectorized for cell array input. Recent versions allow inf/nan values.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Compare Strings (C-convention)
This submission has inspired the following:
String comparison, multisetdiff, jointable

MATLAB release MATLAB 7.5 (R2007b)
Zip File Content  
Other Files lexcmp.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
14 Sep 2009

added support for inf/nan values in arrays and improved help text

Tag Activity for this File
Tag Applied By Date/Time
string Ben Petschel 19 Feb 2009 16:09:49
compare Ben Petschel 19 Feb 2009 16:09:49
comparison Ben Petschel 19 Feb 2009 16:09:49
string comparison Ben Petschel 19 Feb 2009 16:09:49
lexical Ben Petschel 19 Feb 2009 16:09:49
 

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