How can I generate the kind of truth table described below?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
simone clochiatti
le 29 Août 2015
Commenté : Walter Roberson
le 30 Août 2015
Hi, I am having an issue trying to generate this kind of truthtable:
every letter indicates a specific device that it can have 2 or more states, and every device can have just one state active (not two or more active states at the same time):
In this example we have the device a with 2 states and the device b with 3 states, I want to write a script giving just this informations and generate a matrix like this: (with just one and zeros, obviously without a_0 a_1 b_0 b_1 b_2)
a_0 a_1 b_0 b_1 b_2
1 0 1 0 0
1 0 0 1 0
1 0 0 0 1
0 1 1 0 0
0 1 0 1 0
0 1 0 0 1
As you can see every device has only one active state in every combination, I need to generalize this process to an arbitrary number of devices everyone with an arbitrary number of states, and generating all possible combinations with the retrictions that i described above. Thank you very much for your help.
4 commentaires
Star Strider
le 29 Août 2015
Still, it’s not a truth table in the conventional sense I’m familiar with. You’re just defining states, sort of like writing to a register. All you have to do is to set the appropriate states and be sure only one is selected at a time.
Réponse acceptée
Walter Roberson
le 30 Août 2015
numstates = [2, 3, 2]; %three device example
num_orig = length(numstates);
[uniq_numstates, ~, ic] = unique(numstates);
num_uniq = length(uniq_numstates);
state_tables = cell(num_uniq,1);
idx_list = cell(num_uniq,1);
for K = 1 : num_uniq
state_tables{K} = toeplitz([1, zeros(1,uniq_numstates(K)-1)]);
idx_list{K} = 1:uniq_numstates(K);
end
[idxgrid{1:length(numstates)}] = ndgrid(idx_list{ic});
idxlist = cellfun(@(C) C(:), idxgrid, 'uniform', 0);
truth_table = cell2mat(arrayfun(@(K) state_tables{ic(K)}(idxlist{K},:), 1:num_orig, 'uniform',0));
The code optimizes by only creating the unique subtables -- e.g., in the above example the third device has 2 entries just like the first device does, and it is not necessary to build the sliding 1 table multiple times.
Construction of the sliding 1 table could be done incrementally from smallest to largest but it is not clear it would be any faster than the clearer method of calling toeplitz once per size.
The output order might not be exactly how you would prefer, but the list is exhaustive.
8 commentaires
Walter Roberson
le 30 Août 2015
If you had an array C, then C(:) reshapes the contents into a single column vector, exactly the same as reshape(C, [], 1)
@(C) C(:) is an anonymous function that when called with an argument, returns the argument reshaped as a column vector.
cellfun(@(C) C(:), idxgrid, 'uniform', 0)
goes through the cell array "idxgrid" and calls @(C) C(:) on the contents of each entry in the cell array, the same as if you had used
reshape_fun = @(C) C(:);
idxlist = cell(size(idxgrid));
for K = 1 : numel(idxgrid)
idxlist{K} = reshape_fun(idxgrid{K});
end
That is, each entry in the cell array idxgrid is a multidimensional array returned by ndgrid, and the code goes through and makes column vectors out of each of the entries. For example,
{[1 2; 3 4], [5 6; 7 8]}
would become
{[1;3;2;4], [5; 7; 6; 8]}
(Notice that reshaping into column vectors goes down the columns, not across the rows.)
I will describe the logic of the cell2mat() call after I have done some yard work.
Plus de réponses (1)
Image Analyst
le 29 Août 2015
I don't think there's anything that does exactly that, so you might have to play around a little bit with the perms() function, or maybe meshgrid().
1 commentaire
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!