4

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.

prabhat
  • 95
  • 6

3 Answers3

7

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}
Öskå
  • 8,587
  • 4
  • 30
  • 49
6

Use Map:

table = (Most[#] -> Last[#])& /@ arr
molekyla777
  • 2,888
  • 1
  • 14
  • 28
3

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}
Karsten7
  • 27,448
  • 5
  • 73
  • 134