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 :

  1. ge : greater or equal
  2. gt : greater than
  3. le : lower or equal
  4. lt : 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

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -