필터 지우기
필터 지우기

Evaluate many possible logical expressions

조회 수: 5 (최근 30일)
Opt User
Opt User 2017년 6월 10일
댓글: Walter Roberson 2017년 6월 11일
I have a set of binary variables: a,b,c... and a set of rules = { 'a or b', 'a or (b and c)', ... I need a fast way to determine the outcome of rules(i). Creating a truth table is not feasible because the number of binary variables is in the order of ~1000.
  댓글 수: 8
Opt User
Opt User 2017년 6월 11일
Yes, there is a list of variable names (a cell array of strings) and then there is a logical vector corresponding to the state of each of those variables. Furthermore, there is a list of logical rules expressed as strings which use the variable names. Given the logical vector of variable states, the goal is to find an efficient way to evaluate the outcome of each rule.
Walter Roberson
Walter Roberson 2017년 6월 11일
computation_function = matlabFunction(symbolic_expression_list, 'vars', {sym(CellArrayOfVariableNames)} );
result_for_this_vector = computation_function(this_vector_of_logical_values)

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 6월 10일
If you have the symbolic toolbox, you can use sym() to parse the expressions, after which you could use matlabFunction to generate a function handle that can evaluate the expressions on inputs.
You will want to use the matlabFunction 'vars' option to control the order of the inputs; you might want to set it up to take the values as a vector, something like
matlabFunction(symbolic_expression, 'vars', {[a, b, c]}
If you are going to be repeating the calculations many times, you might want to go as far as to tell matlabFunction to write to a file: when it writes to a file then it defaults to optimizing the code, a process that takes a bit of time but might be worth it for repeated execution.
  댓글 수: 3
Opt User
Opt User 2017년 6월 10일
I think using regular expressions I can map each variable in the original string, to to a vector entry ('x[1] or x[2]') and that should do the trick. I will either use a cell of function handles, or like you said, write them down to files for faster performance. Thanks!
Walter Roberson
Walter Roberson 2017년 6월 11일
편집: Walter Roberson 2017년 6월 11일
The syntax I showed,
matlabFunction(symbolic_expression, 'vars', {[a, b, c]}
will convert the names into indexes.
matlabFunction(sym('a | (b and c)'), 'vars', {[sym('a'),sym('b'),sym('c')]})
ans =
function_handle with value:
@(in1)in1(:,1)||(in1(:,2)&&in1(:,3))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by