File Exchange

image thumbnail

strrel

version 1.0.0.0 (1.89 KB) by Ben Barrowes
Applied relational operators to such as == or < to character arrays or cell arrays of strings accord

0 Downloads

Updated 18 Oct 2007

View License

% 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')

Comments and Ratings (3)

Ahmed Fasih

issortedcellchar not found in 2012a.

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

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

1.0.0.0

Faster due to issortedcellchar as suggested by Urs (us) Schwarz.

Small bugfix.

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux