from
Truth Table Generation
by Mahendra
This function simply computes the truth table for a given number of variables.
|
| TruthTable(n)
|
function ttable = TruthTable(n)
% This function computes the truth table for a given number of variables
% ttable = TruthTable(n)
% For n logical variables, 2^n possibilities exist.
% For example for three variables, the expected truth table is:
% 0 0 0
% 1 0 0
% 0 1 0
% 1 1 0
% 0 0 1
% 1 0 1
% 0 1 1
% 1 1 1
numel = 2^n; % Number of combinations
ttable = false(2^n,n); % Pre-allocate the elements of the truth table
for ii = 1:n; % The columns of the truth table
tempel = [false(2^(ii-1),1);true(2^(ii-1),1)]; % A particular column primitive according to column number ii
ttable(:,ii) = repmat(tempel,[numel/2^ii 1]); % Replicating the primitive element and saving into the truth table variable according to column
end
|
|
Contact us at files@mathworks.com