File Exchange

## strrel

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

Updated 18 Oct 2007

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

### Cite As

Ben Barrowes (2020). strrel (https://www.mathworks.com/matlabcentral/fileexchange/16894-strrel), MATLAB Central File Exchange. Retrieved .

Ahmed Fasih

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