How would you approach this problem?

Hi there,
I want my program to follow the following truth table:
tabla de verdad.PNG
"actual" and "next" columns refer to the following full table:
tabla de excitacion.PNG
So, for example first row, y2 and y2' are both 0, so according to the truth table, J2 has to be 0 and K2 X. Another one, 4th row (nº 3), Y1 and Y1' are 1 and 0, so J must be X and K 1.
I only have been able to get matrices (cc, cu, uc, uu) that show if the values from actual and next follow the truth table:
dec = 0:7;%Decimal values the counter will follow
a = de2bi(dec, 'left-msb'); %actual state of the counter
s = a([2 3 4 5 6 7 8 1],:); %next state of the counter
cc = a==0 & s==0; %values for J = 0
cu = a==0 & s==1; %values for J = 1
uc = a == 1 & a == 0; %values for J = X
uu = a == 1 & a == 1; %values for J = X
In order to get the J and K values I need to make Matlab follow the orders of the truth table, and I don't know how to continue.
I don't like loops, so does Matlab, so I would like to make some sort of "if" function or something similar to get this to work.
Finally, I would only need some orientation about this, don't want to see the code fully written here, just a hint on how you would tackle this.
Thank you.

1 Comment

For those that don't have the communications toolbox:
a = dec2bin(0:7)-'0';

Sign in to comment.

 Accepted Answer

Daniel M
Daniel M on 31 Oct 2019
Edited: Daniel M on 31 Oct 2019
I don't understand the second truth table, but the first one can be done as follows:
actual = [0;0;1;1];
next = [0;1;0;1];
x = 2;
J = actual*x + ~actual.*next;
K = ~actual*x + actual.*~next;
If x is a row vector, J and K will have as many columns as does x.

7 Comments

I think maybe an explanation of what this is for would be useful:
The second table describes how a module 8 ascent JK counter works. Basically, it is a device that counts from 0 to 7.
In order to make it work, you have to provide some entries to the counter (J and K), and in this example the device has 3 JK entries (J0/K0, J1/K1 and J2/K2).
The table can be understood as an instruction guide that shows what values of JK you should provide to the device to make it count from 0 to 7.
So, I need to take all those actual and next values, pass them through the first table, and then obtain the JK values.
I have thought of using the following if statement, but I don't know how to tune it for matrices instead of vectors:
This comes from this info .
Yeah I don't follow. Please phrase it in terms of inputs and outputs to your function, what you want the function to do, and give several specific examples. Did my previous solution make any sense to you?
I finally made a loop to achive it.
dec = 0:7;%Decimal values the counter will follow
a = de2bi(dec, 'left-msb'); %actual state of the counter
s = a([2 3 4 5 6 7 8 1],:); %next state of the counter
est= [a s];
fil=8;
col=6;
JK = zeros(fil,col);
for c=1:col/2
for f=1:fil
if (est(f,c) == 0 & est(f,c+3) == 0)
JK(f,2*c-1) = 0;
JK(f,2*c) = NaN;
elseif (est(f,c) == 0) & (est(f,c+3) == 1)
JK(f,2*c-1) = 1;
JK(f,2*c) = NaN;
elseif (est(f,c) == 1) & (est(f,c+3) == 0)
JK(f,2*c-1) = NaN;
JK(f,2*c) = 1;
elseif (est(f,c) == 1) & (est(f,c+3) == 1)
JK(f,2*c-1) = NaN;
JK(f,2*c) = 0;
end
end
end
The thing is I did not want a loop, but I haven't found any other way to make it possible.
Alright, now I understand. You have three truth tables combined in one. y2 and y2' correspond with j2 and k2. These have nothing to do with any of the other columns (y1, j1, etc.). And I am assuming, since you do not want a loop, you want a function that will return the proper jx/kx given an input yx/yx'. OK, let me work on that.
Yeah exactly that. Thank you for your help.
Guess what? It is the exact same logic that I posted in my first answer.
Here it is:
function [J,K] = JKcounter(y,yp)
% This function returns the value for JK for a given y and yp (y-prime)
% input.
% For example: [J,K] = JKcounter(0,0); % J = 0, K = NaN;
% y and yp can be matrix input.
x = 2; % because NaN is difficult to work with.
J = y*x + ~y.*yp;
K = ~y*x + y.*~yp;
J(J==x) = NaN;
K(K==x) = NaN;
end
I've also attached a script showing in detail that this works, and how to use it.
Well, thank you very much for your time and help. Much appreciated.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!