Find a boolean expression from a truth table (several bits) - boolean

I have the following truth table (a and b being my inputs and r the result):
a
b
r
00
00
00
00
01
01
00
11
01
01
00
01
01
01
01
01
11
01
11
00
01
11
01
01
11
11
11
The issue is that I can't find the boolean expression to express this truth table.
Another similar thread pointed out that karnaugh maps could solve it, but I can't find any implementation working with several bits inputs.
Note that to my model, the second bit doesn't matter is the first one is set for a specific input, thus if it facilitates the boolean expression, I can force it to 0, to 1, or not even force it.

Truth Table (given):
a0 a1 b0 b1 r0 r1
0 0 0 0 0 0
0 0 0 1 0 1
0 0 1 1 0 1
0 1 0 0 0 1
0 1 0 1 0 1
0 1 1 1 0 1
1 1 0 0 0 1
1 1 0 1 0 1
1 1 1 1 1 1
Kmaps:
r0:
\a0a1
b0b1 \ | 00| 01 11 10
00 | 0 | 1 1 1
---
01 1 1 1 1
11 1 1 1 1
---
10 | x | x x x
| |
r1:
\a0a1
b0b1 \ 00 01 11 10
00 0 0 0 0
01 0 0 0 0
---
11 0 0 | 1 | 0
| |
10 x x | x | x
---
Boolean Expressions:
r0 = a0 + a1 + b1
r1 = a0a1b0

Related

How can I make a diamond of zeroes in a matrix of any size? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a square Matrix N x M, odd dimensions, and I want to put a diamond of zeroes, for example, for a 5 x 5 matrix:
1 3 2 4 2
5 7 8 9 5
3 2 4 6 3
6 8 2 1 3
3 3 3 3 3
Is transform to:
1 3 0 4 2
5 0 8 0 5
0 2 4 6 0
6 0 2 0 3
3 3 0 3 3
How can this be done efficiently?
I'll bite, here is one approach:
% NxN matrix
N = 5;
assert(N>1 && mod(N,2)==1);
A = magic(N);
% diamond mask
N2 = fix(N/2);
[I,J] = meshgrid(-N2:N2);
mask = (abs(I) + abs(J)) == N2;
% fill with zeros
A(mask) = 0;
The result:
>> A
A =
17 24 0 8 15
23 0 7 0 16
0 6 13 20 0
10 0 19 0 3
11 18 0 2 9
I also had some time to play around. For my solution there are no limits concerning A being odd or even or larger than 1. Every integer is fine (even 0 works, though it does not make sense).
% NxN matrix
N = 7;
A = magic(N);
half = ceil( N/2 );
mask = ones( half );
mask( 1 : half+1 : half*half ) = 0;
mask = [ fliplr( mask ) mask ];
mask = [ mask; flipud( mask ) ];
if( mod(N,2) == 1 )
mask(half, :) = []
mask(:, half) = []
end
A( ~mask ) = 0;
A
I am first creating a square sub-matrix mask of "quarter" size (half the number of columns and half the number of rows, ceil() to get one more in the case N is odd).
Example for N=7 -> half=4.
mask =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
I then set it's diagonal values to zero:
mask =
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
Mirror the mask horizontally:
mask =
1 1 1 0 0 1 1 1
1 1 0 1 1 0 1 1
1 0 1 1 1 1 0 1
0 1 1 1 1 1 1 0
Then mirror it vertically:
mask =
1 1 1 0 0 1 1 1
1 1 0 1 1 0 1 1
1 0 1 1 1 1 0 1
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
1 0 1 1 1 1 0 1
1 1 0 1 1 0 1 1
1 1 1 0 0 1 1 1
As N is odd we got a redundant row and redundant column that are then removed:
mask =
1 1 1 0 1 1 1
1 1 0 1 0 1 1
1 0 1 1 1 0 1
0 1 1 1 1 1 0
1 0 1 1 1 0 1
1 1 0 1 0 1 1
1 1 1 0 1 1 1
The logical not is then used as a mask to select the values in the original matrix that are set to 0.
Probably not as efficient as #Amro's solution, but it works. :D
My solution:
looking at the first left half of the matrix
in the first row 0 is in the middle column (let's call it mc)
in the second row the 0is in column mc-1
and so on while the rows increase
when you reach column 1 the sequence continue but with mc+1 but the rows decrease
In a similar way for the right half of the matrix
n=7
a=randi([20 30],n,n)
% Centre of the matrix
p=ceil(n/2)
% Identify the column sequence
col=[p:-1:1 2:p p+1:n n-1:-1:p]
% Identify the row sequence
row=[1:n n-1:-1:1]
% Transorm the row and column index in linear index
idx=sub2ind(size(a),row,col)
% Set the 0'
a(idx)=0
a =
22 29 23 27 27 21 23
29 29 21 27 24 26 24
30 28 21 27 29 28 25
28 22 24 20 27 24 25
23 26 21 20 30 20 29
26 20 26 23 25 22 25
21 24 25 25 23 21 30
a =
22 29 23 0 27 21 23
29 29 0 27 0 26 24
30 0 21 27 29 0 25
0 22 24 20 27 24 0
23 0 21 20 30 0 29
26 20 0 23 0 22 25
21 24 25 0 23 21 30
Hope this helps.
Qapla'
Using indexing (only works when N is odd):
N = 7;
% Random matrix
A = randi(100, N);
idx = [N-1:-2:1; 2:2:N];
A(cumsum([ceil(N/2) idx(:)' idx(end-1:-1:1)])) = 0
A =
60 77 74 0 54 83 9
8 48 0 76 0 28 67
6 0 32 78 83 0 10
0 27 25 5 11 39 0
76 0 49 43 67 0 16
79 7 0 86 0 70 78
57 28 85 0 81 44 81

delete specific columns when other column has specific value (perl or awk)

I have a file with 16 different columns (tab-separated values):
22 51169729 G 39 A 0 0 C 0 0 G 38 0.974359 T 1 0.025641
22 51169730 A 36 A 36 1 C 0 0 G 0 0 T 0 0
22 51169731 C 39 A 0 0 C 39 1 G 0 0 T 0 0
22 51169732 G 37 A 0 0 C 0 0 G 37 1 T 0 0
22 51169733 G 33 A 0 0 C 0 0 G 33 1 T 0 0
22 51169734 C 35 A 0 0 C 35 1 G 0 0 T 0 0
22 51169735 A 32 A 32 1 C 0 0 G 0 0 T 0 0
22 51169736 G 32 A 0 0 C 0 0 G 32 1 T 0 0
22 51169737 C 30 A 0 0 C 30 1 G 0 0 T 0 0
22 51169738 T 27 A 0 0 C 0 0 G 0 0 T 27 1
22 51169739 G 26 A 0 0 C 0 0 G 26 1 T 0 0
22 51169740 A 25 A 25 1 C 0 0 G 0 0 T 0 0
22 51169741 C 22 A 0 0 C 22 1 G 0 0 T 0 0
22 51169742 G 23 A 0 0 C 0 0 G 23 1 T 0 0
22 51169743 C 21 A 0 0 C 21 1 G 0 0 T 0 0
22 51169744 C 22 A 0 0 C 22 1 G 0 0 T 0 0
22 51169745 C 19 A 0 0 C 19 1 G 0 0 T 0 0
22 51169746 C 19 A 0 0 C 19 1 G 0 0 T 0 0
22 51169747 A 15 A 14 0.933333 C 1 0.0666667 G 0 0 T 0 0
22 51169748 C 20 A 0 0 C 20 1 G 0 0 T 0 0
The third column can be A, G, C or T.
I would like to:
remove columns 5, 6 and 7 when column 3 is an 'A' OR when $7=='0'.
Similarly, remove columns 8, 9, 10 when $3== 'C' OR when $10=='0'.
remove columns 11, 12, 13 when $3=='G' OR when $13=='0'.
and remove columns 14, 15, 16 when $3=='T' OR when $16=='0'.
When this is done for the entire file, there would only be 4 columns left in some cases and 7 columns in other cases, like in the following example:
22 51169729 G 39 T 1 0.025641
22 51169730 A 36
22 51169731 C 39
22 51169732 G 37
22 51169733 G 33
22 51169734 C 35
22 51169735 A 32
22 51169736 G 32
22 51169737 C 30
22 51169738 T 27
22 51169739 G 26
22 51169740 A 25
22 51169741 C 22
22 51169742 G 23
22 51169743 C 21
22 51169744 C 22
22 51169745 C 19
22 51169746 C 19
22 51169747 A 15 C 2 0.133333
22 51169748 C 20
Any suggestions?
Perl solution for the first part:
#!/usr/bin/perl
use warnings;
use strict;
my %remove = ( A => 4, # Where to start removing the columns
C => 7, # for a given character in column #3.
G => 10,
T => 13,
);
$\ = "\n"; # Add newline to prints.
$, = "\t"; # Separate values by tabs.
while (<>) { # Read input line by line;
chomp; # Remove newline.
my #F = split /\t/; # Split on tabs, populate an array.
splice #F, $remove{ $F[2] }, 3; # Remove the columns.
print #F; # Output.
}
Once you clarify the second requirement, I can try to add more code. What values do you want to remove? Can you show more examples?
Here's one way to do the first part, assuming no empty fields:
$ cat tst.awk
$3 == "A" { $5=$6=$7="" }
$3 == "C" { $8=$9=$10="" }
$3 == "G" { $11=$12=$13="" }
$3 == "T" { $14=$15=$16="" }
{ gsub(/[[:space:]]+/,"\t"); print }
$ awk -f tst.awk file
1 957584 C 157 A 1 0.006 G 0 0 T 0 0
I don't really understand what you're trying to do in the 2nd part but it sounds like this might be what you want if the test on $7/10/13 is the modified field numbers after the first phase:
$3 == "A" { $5=$6=$7="" }
$3 == "C" { $8=$9=$10="" }
$3 == "G" { $11=$12=$13="" }
$3 == "T" { $14=$15=$16="" }
{ $0=$0 }
$7 ~ /0/ { c++ }
$10 ~ /0/ { c++ }
$13 ~ /0/ { c++ }
c > 1 { $8=$9=$10="" }
{ c=0; gsub(/[[:space:]]+/,"\t"); print }
or this if the test on $7/10/13 is the original field numbers:
$7 ~ /0/ { c++ }
$10 ~ /0/ { c++ }
$13 ~ /0/ { c++ }
$3 == "A" { $5=$6=$7="" }
$3 == "C" { $8=$9=$10="" }
$3 == "G" { $11=$12=$13="" }
$3 == "T" { $14=$15=$16="" }
c > 1 { $8=$9=$10="" }
{ c=0; gsub(/[[:space:]]+/,"\t"); print }
If not, edit your question to clarify with a better example.

Circuit that accept 4-bit number and generate its triple, what does that mean? Can someone give me an example of it?

I have to design a combinational circuit that accepts a 4-bit number and generate its triple, what does it mean? Can someone please give me an example of a specific input and its output so I can understand the question?
If you could give me also any hint for designing this circuit I would be very grateful.
Thank you.
You'll want to make a truth table and then derive the combinational logic from either boolean algebra (sum of truths), or from a k-map.
If, for example, the input is 0100 which is decimal 4, then the triple would be 12, or 1100. Since the highest number is 1111 (15), then your output has to be able to represent 45, or 101101 (6 bits).
Hence, you'll have something like:
Input | Output
-----------------
abcd uvwxyz
0000 | 000000
0001 | 000011
0010 | 000110
0011 | 001001
0100 | 001100
0101 | 001111
0110 | 010010
0111 | 010101
1000 | 011000
1001 | 011011
1010 | 011110
1011 | 100001
1100 | 100100
1101 | 100111
1110 | 101010
1111 | 101101
From that you can build a k-map for each output bit and find the minimum combinational logic required per output bit.
For example, to find the combinational logic for bit u then you would use the following k-map:
AB
00 01 11 10
CD 00 0 0 1 0
01 0 0 1 0
11 0 0 1 1
10 0 0 1 0
Which reduces to ACD + AB
Repeat for the other 5 bits (v-z) and you'll have the full combinational logic needed to implement the solution.
Start with a truth table:
IN OUT
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 1 3 0 0 0 0 1 1
2 0 0 1 0 6 0 0 0 1 1 0
3 0 0 1 1 9 0 0 1 0 0 1
4 0 1 0 0 C 0 0 1 1 0 0
5 0 1 0 1 F 0 0 1 1 1 1
6 0 1 1 0 12 0 1 0 0 1 0
7 0 1 1 1 15 0 1 0 1 0 1
8 1 0 0 0 18 0 1 1 0 0 0
9 1 0 0 1 1B 0 1 1 0 1 1
A 1 0 1 0 1E 0 1 1 1 1 0
B 1 0 1 1 21 1 0 0 0 0 1
C 1 1 0 0 24 1 0 0 1 0 0
D 1 1 0 1 27 1 0 0 1 1 1
E 1 1 1 0 2A 1 0 1 0 1 0
F 1 1 1 1 2D 1 0 1 1 0 1
Then use a standard technique such as a Karnaugh Map to deduce the input/output expressions.

strange result with JPEG compression

I want to implement the JPEG compression by using MATLAB. Well at the point where the symbols' probabilities (Huffman coding) are calculated i can see some NEGATIVE values. I am sure that this is not correct!!! if someone can give some help or directions i would really appreciate it. Thank all of you in advance. I use MATLAB R2012b. Here is the code:
clc;
clear all;
a = imread('test.png');
b = rgb2gray(a);
b = imresize(b, [256 256]);
b = double(b);
final = zeros(256, 256);
mask = [1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0
1 1 1 1 1 1 0 0
1 1 1 1 1 0 0 0
1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0];
qv1 = [ 16 11 10 16 24 40 51 61
12 12 14 19 26 58 60 55
14 13 16 24 40 57 69 56
14 17 22 29 51 87 80 62
18 22 37 56 68 109 103 77
24 35 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99];
t = dctmtx(8);
DCT2D = #(block_struct) t*block_struct.data*t';
msk = #(block_struct) mask.*block_struct.data;
for row = 1:8:256
for column = 1:8:256
x = (b(row:row+7, column:column+7));
xf = blockproc(x, [8 8], DCT2D);
xf1 = blockproc(xf, [8 8], msk);
xf1 = round(xf1./qv1).*qv1;
final(row:row+7, column:column+7) = xf1;
end
end
[symbols,p] = hist(final,unique(final));
bar(p, symbols);
p = p/sum(p); %NEGATIVE VALUES????
I think you might have the outputs of hist (symbols and p) swapped. The probability should be calculated from the bin counts, which is the first output of hist.
[nelements,centers] = hist(data,xvalues) returns an additional row vector, centers, indicating the location of each bin center on the x-axis. To plot the histogram, you can use bar(centers,nelements).
In other words, instead of your current line,
[symbols,p] = hist(final,unique(final));
just use,
[p,symbols] = hist(final,unique(final));
Also, final is a matrix rather than a vector, so nelements will be a matrix:
If data is a matrix, then a histogram is created separately for each column. Each histogram plot is displayed on the same figure with a different color.

Matlab: how to solve a binary system of equations?

I'm trying to solve a system of equation with the following form:
a5 + a6 + a7 + f5 + f6 + f7 = 11;
b5 + b6 + b7 + e5 + e6 + e7 = 100;
c5 + c6 + c7 + d5 + d6 + d7 = 100;
a5 + b5 + c5 + d5 + e5 + f5 = 11;
a6 + b6 + c6 + d6 + e6 + f6 = 100;
a7 + b7 + c7 + d7 + e7 + f7 = 100;
where all the variables and digits are binaries.
Is there a way to do this in Matlab?
For example, by substituting the binary numbers by there decimal values:
a5 + a6 + a7 + f5 + f6 + f7 = 3;
b5 + b6 + b7 + e5 + e6 + e7 = 4;
c5 + c6 + c7 + d5 + d6 + d7 = 4;
a5 + b5 + c5 + d5 + e5 + f5 = 3;
a6 + b6 + c6 + d6 + e6 + f6 = 4;
a7 + b7 + c7 + d7 + e7 + f7 = 4;
and tell Matlab somehow that the unknowns should be integers and from the interval [0:1]?
Here is the A x = b form:
A = [1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1;
0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0;
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0;
0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0;
0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1];
b = [3 4 4 3 4 4];
what would be then the next step?
bintprog, if you have the optimization toolbox.
Since there are likely multiple solutions, I'll choose the solution which has the minimum sum.
>> A = [1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1;
0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0;
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0;
0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0;
0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1];
>> b = [3 4 4 3 4 4];
>> bintprog(ones(1,18),[],[],A,b)
Optimization terminated.
ans =
0
1
1
1
1
1
1
1
1
1
0
0
0
0
1
0
1
0