Evaluate many possible logical expressions

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 Commenti

Are the binary variables definite values that you want to use to evaluate the rules at?
What form are the rules in? Are they literally character vectors like 'a or (b and c)' that would each need to be examined to figure out the corresponding MATLAB code?
The binary variables will take on definite values in order to evaluate the rules. The rules are literally in the form I expressed them in my question.
I am considering that creating a truth table for each rule which only contains the variables involved in that rule may be an option. Since in general each rule has around 2 to 5 variables, and the longest ones may have 20. But I wonder if this is worth it, meaning, creating such tables and looking them up being faster than using eval on the rules.
Stephen23
Stephen23 il 10 Giu 2017
Modificato: Stephen23 il 10 Giu 2017
@Opt User: some questions:
  1. How did you get ~1000 variables into your workspace?
  2. Is there a good reason why these variables are not simply stored in one array? (After all, the name MATLAB comes from MATrix LABoratory, and not from "lets store our data in a thousand separate variables". Keeping data together as much as possible makes program design much simpler).
  3. What scheme do the variable names follow? Your examples show a, b, and c, but this limits the total to 26. What are the other 974 variables called?
  4. Where/how are these conditions defined? Is it possible to change their format/style/syntax?
The variables are in a logical vector. The conditions are defined in a cell array of strings like I illustrated before.
Take element #314 of the logical vector. What variable name does it correspond to? Is there a list of variable names, or is it something that is calculated, similar to Excel column naming ?
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.
computation_function = matlabFunction(symbolic_expression_list, 'vars', {sym(CellArrayOfVariableNames)} );
result_for_this_vector = computation_function(this_vector_of_logical_values)

Accedi per commentare.

 Risposta accettata

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 Commenti

I like this. Is there a way I can avoid the actual variable names so the resulting functions are as follows:
function out1 = rule_1(x)
out1 = x(1) || x(2);
Right now I am getting something along the lines of :
function out1 = rule_1(a,b,c,d,...)
out1 = a || b;
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!
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))

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by