How can I generate the kind of truth table described below?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Star Strider
2015년 8월 29일
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.
채택된 답변
Walter Roberson
2015년 8월 30일
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
Walter Roberson
2015년 8월 30일
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.
추가 답변 (1개)
Image Analyst
2015년 8월 29일
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().
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!