4
$\begingroup$

I am new to Mathematica, and would like some help in the following operation. I have a truth table stored in a text file. I want to read this in Mathematica, and find its Boolean Rule. Suppose I have two Boolean variables. I am reading the text file in Mathematica like this:

arr=Import["bool.txt","Table"]

Suppose this array is arr=

0 0 0
0 1 1
1 0 1
1 1 1

The third column represents the output, and the first two columns are the states of the inputs. For finding the Boolean rule, I need to convert this array as an association of

table={{0,0}->0,{0,1}->1,{1,0}->1,{1,1}->1}

So that I can infer the Boolean rule using the following function:

BooleanFunction[table,{x,y}]

My problem is, I don't know how to create the variable table, from the array arr. I looked up a bit and think the Association function would be used, but not sure how. I need to do it for multiple text files, and am looking to develop a short program for this functionality. Any help would be appreciated.

$\endgroup$

3 Answers 3

6
$\begingroup$

Use Map:

table = (Most[#] -> Last[#])& /@ arr
$\endgroup$
1
  • $\begingroup$ Thanks a lot, it is as good as the above method. $\endgroup$
    – prabhat
    Nov 6, 2014 at 0:38
7
$\begingroup$

You can simply do:

{#, #2} -> #3 & @@@ {{0, 0, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1}}
{{0, 0} -> 0, {0, 1} -> 1, {1, 0} -> 1, {1, 1} -> 1}
$\endgroup$
1
  • $\begingroup$ Thanks a lot! So simply a line of code is sufficient I see {#, #2} -> #3 & @@@ arr. $\endgroup$
    – prabhat
    Nov 6, 2014 at 0:37
3
$\begingroup$

You could also use Thread:

table = Thread[arr[[All, ;; 2]] -> arr[[All, -1]]]
{{0, 0} -> 0, {0, 1} -> 1, {1, 0} -> 1, {1, 1} -> 1}
$\endgroup$
1
  • $\begingroup$ Very useful to know, thanks :) $\endgroup$
    – prabhat
    Nov 6, 2014 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.