# Dynamically changing a binary array with BinaryArraySetter

**URL:** <https://community.wolfram.com/t/dynamically-changing-a-binary-array-with-binaryarraysetter/229>\
**Category:** Ideas\
**Tags:** ruliology, mathematica, interface-construction\
**Created:** [July 9, 2013, 3:50am UTC](https://community.wolfram.com/t/dynamically-changing-a-binary-array-with-binaryarraysetter/229 "2013-07-09T03:50:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taliesinb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/taliesinb/32/39832_2.png) [@taliesinb](https://community.wolfram.com/u/taliesinb)\
**Post date:** [July 9, 2013, 3:50am UTC](https://community.wolfram.com/t/dynamically-changing-a-binary-array-with-binaryarraysetter/229/1 "2013-07-09T03:50:06Z")

</div>

At the Wolfram SS summer school, we often find it useful to play around with intial conditions for simple computational systems like Turing machines and cellular automata.

In these cases, it is convenient to be able to interactively set the elements of a binary vector or matrix.

Here is some self-contained code that I recently wrote that achieves just this:

```auto
SetAttributes[BinaryArraySetter, HoldFirst];
BinaryArraySetter[x_Symbol,opts___] := With[
    {dims = Dimensions[x],
     sz = OptionValue[{opts,PixelConstrained->False},PixelConstrained]}, With[
    {ndim = Length[dims]}, 
    If[1 <= ndim <= 2, 
    EventHandler[
        Dynamic[
            ArrayPlot[
                If[ndim == 1, {x}, x], 
                opts, Mesh -> True, Frame->True, 
                PixelConstrained -> sz],
            TrackedSymbols :> {x}
        ],
        {"MouseClicked" :> With[
            {v = Ceiling @ Apply[{Last[dims]-#2,#1}&,
                MousePosition["Graphics"]/Replace[sz, False -> 1]]},
            If[ArrayQ[x, 1|2, IntegerQ] && 1 <= First[v] <= First[dims] && 1 <= Last[v] < Last[dims],
                With[{v2 = Sequence @@ If[ndim == 1, Last, Identity] @ Ceiling[v]},
                    x[[v2]]=1-x[[v2]]
                ]
            ]
        ]}
    ],
    Return[$Failed]
]]];

```

Use it in the following way:

```auto
x = RandomInteger[1, {15, 15}];
BinaryArraySetter[x, PixelConstrained -> 10]

```

By clicking on individual cells, you can toggle the corresponding element of the array between 0 and 1. The array can be 1 or 2 dimensional.

Here’s a picture:

 ![ScreenShot2013-07-08at11.49.04PM.png](https://global.discourse-cdn.com/wolfram/original/3X/5/b/5b62116b8591183252ac8bbe42c6eb1defc7f629.png)

Hope someone else finds this useful!

---

<div class="post-metadata">

**Author:** ![Vitaliyk](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/vitaliyk/32/45591_2.png) [@Vitaliyk](https://community.wolfram.com/u/Vitaliyk)\
**Post date:** [July 9, 2013, 5:39am UTC](https://community.wolfram.com/t/dynamically-changing-a-binary-array-with-binaryarraysetter/229/2 "2013-07-09T05:39:45Z")

</div>

Very neat, Tali. I also did something similar in [this answer](http://mathematica.stackexchange.com/questions/22556). The value of the matrix is stored in the variable tds.

```auto
Manipulate[x=ConstantArray[0,9 {1,1}]; 
   Row[{EventHandler[Dynamic[tds=Reverse[Transpose[x]]; 
   MatrixPlot[tds,PlotRangePadding->0,Mesh->All,ImageSize->{300,300},ColorRules->{1->Black,0->None}]], 
   {"MouseClicked":>(pos=Ceiling[MousePosition["Graphics"]]; 
   x=ReplacePart[x,pos->1-x[[Sequence@@pos]]];)}], 
   Dynamic@Magnify[MatrixForm[tds],2]}],
{pos,ControlType->None},
{x,ControlType->None},
AppearanceElements->None,FrameMargins->0]

```

 ![6481ScreenShot2013-07-09at1.38.37AM.png](https://global.discourse-cdn.com/wolfram/original/3X/c/a/ca839fabd214d4d5a347dcf0567604e88fb4e30c.png)

---

<div class="post-metadata">

**Author:** ![pbarendse](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/pbarendse/32/30486_2.png) [@pbarendse](https://community.wolfram.com/u/pbarendse)\
**Post date:** [July 10, 2013, 3:27pm UTC](https://community.wolfram.com/t/dynamically-changing-a-binary-array-with-binaryarraysetter/229/3 "2013-07-10T15:27:30Z")

</div>

Thanks, this is useful
