You can do this pretty easily if you don't have too many variables.
This method will stall if you do have many variables, because it uses use a matrix of size K*(2^K)
, where K
is the number of variables, and combvec
gets pretty slow for large K
too.
Whilst you have to be wary of the number of variables, this method is pretty capable of handling many logical 'equations' with little overhead.
In the x
, y
, z
example:
% Get all combinations of x/y/z, where each is true or false
opts = repmat( {[true, false]}, 1, 3 );
xyz = combvec( opts{:} )
% Assign each row to a named variable
x = xyz(1,:); y = xyz(2,:); z = xyz(3,:);
% Get the combinations which satisfy your conditions
results = xyz( :, (x & y) & (x | z) );
% Each column of results is a solution
>> results
results =
1 1
1 1
1 0
Written more generally, it might look something like this:
K = 3; % K variables (previously x, y and z so K = 3)
% Create all true/false combinations
opts = repmat( {[true, false]}, 1, K );
combs = combvec( opts{:} );
% Shorthand so we can write in(i) not combs(i,:)
in = @(k) combs(k,:);
% Apply conditions
results = combs( :, (in(1) & in(2)) ...
& (in(1) | in(3)) );
Note: if you don't have the Neural Network Toolbox, you won't have combvec
. There are many alternatives for getting all the combinations.