How can I generate the kind of truth table described below?

6 views (last 30 days)
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 Comments
Star Strider
Star Strider on 29 Aug 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.
simone clochiatti
simone clochiatti on 29 Aug 2015
Edited: simone clochiatti on 29 Aug 2015
exatly that is, but pratically how can you manually set every possible combination of states of every device if there are too many and with many states for each one?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Aug 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 Comments
simone clochiatti
simone clochiatti on 30 Aug 2015
Edited: simone clochiatti on 30 Aug 2015
Thank you, I understood how it works, I still have problems on the last 2 rows of the code, in cell2mat I expected to have a function in the first element, but what does it mean @(C) C(:) ?? I checked in the documentation but I cant find anything, another problem that I have is with the function ndgrid (I have studied how it behaves,but is still not clear for me how you use it in this problem) It is strange because the main part of the code is very simple for me, and I feel to guess why you generate the digonal matrix; but the last three rows of code are completely unknown for me. Is it possible to simplify that 3 rows of code? making them more suitable for a beginner? I dont know if the problem for me is understanding the code or is just that I dont understand how you generate the table from that multiple diagonal matrix's you create by using the for cycle. Thank you so much for your help.
Walter Roberson
Walter Roberson on 30 Aug 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.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 Aug 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 Comment
simone clochiatti
simone clochiatti on 29 Aug 2015
Edited: simone clochiatti on 29 Aug 2015
Thank you for your advice, but sorry I tryed until now with perms and I cant find a solution: I have generated this vector:
0 1 0 1 0 1 0 0 0 1
this means 3 devices with 2 states ( the first 6 elements) and 1 device with 4 possible states (the last 4 elements) then I have generated a matrix with all possible permutations using perms. Now I am trying an approach to eliminate the rows of that matrix with the ones in the wrong position such as:
1 1 0 1 1 0 0 0 0 0 (because the device 1 have the 2 states active at the same time, and is impossible that the device 4 has none active states)
0 1 1 1 1 0 0 0 0 0 (because the device 2 have the 2 states active at the same time, and is impossible that the device 4 has none acrive states)
1 0 0 0 1 0 0 1 1 0 (because the device 4 has two states active at the same time, and of course is impossible that the device 2 has the 2 states off at the same time)
I have the matrix and now the problem is to eliminate all the kind of rows that I described above. I will be so glad of you could help me, thank you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!