matlab - find indices according to input string operator -
lets suppose have vector in function
b = 1:100
the input function condition , threshold ('<' , 10)
and function returns indices greater , greater equal , equal etc
a conventional way make list of ifs
if(strcmp('>',condition)) indices = find(b > threshold)
for each operator if want in 1 line if input condition greater >
operater find()
function finds b greater threshold instead of making if each operator
as state in comments, using eval
not pratice. however, passing operators strings force so, meaning either have use it, or you'll have change inputs function.
if don't want forced use eval
, instead of passing string representing operator function, you'd rather want pass directly handle 1 of these functions :
ge
: greater or equalgt
: greater thanle
: lower or equallt
: lower
the function (i'll let error/wrong input checking) :
function out=myfun(funhandle,threshold) b=1:100; out=find(funhandle(b,threshold)); end
outputs :
myfun(@ge,90) columns 1 through 8 90 91 92 93 94 95 96 97 columns 9 through 11 98 99 100 myfun(@lt,12) columns 1 through 8 1 2 3 4 5 6 7 8 columns 9 through 11 9 10 11
Comments
Post a Comment