Code covered by the BSD License  

Highlights from
strrel

5.0

5.0 | 1 rating Rate this file 8 Downloads (last 30 days) File Size: 1.89 KB File ID: #16894

strrel

by

 

15 Oct 2007 (Updated )

Applied relational operators to such as == or < to character arrays or cell arrays of strings accord

| Watch this File

File Information
Description

% function out=strrel(in1,in2,relop)
%
% Compares strings according to strcmp and issorted according to relop
% in1 and in2 can be character arrays or cell arrays
% relop can be <, <=, >, >=, ==, and ~=, or their string equivalents
% such as 'ne' or 'gt', etc.
%
% Examples:
% c = {'How','much','wood','would','a','woodchuck','chuck?'};
% s = 'wood';
% r = strrel(s,c,'<')
% r = strrel(s,c,'gt')

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 (3)
13 Jun 2012 Ahmed Fasih

issortedcellchar not found in 2012a.

17 Oct 2007 Urs (us) Schwarz

since you seem to listen to your customers, i now show you the fastest solution, which will drop the execution time to ~25% of your current version:

% the crucial part of the engine
out=false(size(in1));
ie =strcmp(in1,in2);
for i=1:numel(in1)
is=issortedcellchar({in1{i},in2{i}}); %!!!
switch lower(relop)
case {'<','lt'}
out(i)=is & ~ie(i);
case {'<=','le'}
out(i)=is | ie(i);
case {'==','eq'}
out(i)=ie(i);
case {'~=','ne','!=','/='}
out(i)=~ie(i);
case {'>','gt'}
out(i)=~is & ~ie(i);
case {'>=','ge'}
out(i)=~is | ie(i);
otherwise
error([relop,' is not recognized'])
end
end

also, being an academic yourself, you certainly know how to acknowledge the contributions of others to your work, which may not simply be a SMALL BUGFIX as mentioned earlier...

us

16 Oct 2007 urs (us) schwarz

nice idea... a few remarks
1) the help section needs a bit of work, eg, H1 and syntax
2) the last switch statement should read GE instead of GT
3) the engine could be vectorized, eg
t={the token};
c=the cellstring;
ie=strcmp(t,c);
is=cellfun(@(x) issorted([t,{x}]),c);
% the swith, eg, 'gt'
out=~is & ~ie

just some thoughts
us

Updates
17 Oct 2007

Small bugfix.

Contact us