Function Repository Resource:

PerimeterMaskArray (1.0.0) current version: 1.0.1 »

Source Notebook

Create an array with ones around the perimeter

Contributed by: Phileas Dazeley-Gaist

ResourceFunction["PerimeterMaskArray"][dims,r]

creates an array of dimensions dims where cells within r cells of the border are 1, and all other cells are 0.

ResourceFunction["PerimeterMaskArray"][dims]

creates an array where only border cells are 1, else 0.

Details

The radius r defaults to 1.
When , zeros fill out the whole resulting array.
When r>=(dim/2), ones fill out the whole resulting array.
When the piecewise equation is satisfied for all dimensions , the radius is small enough so that the resulting array has an outer shell of ones of width r, and an inner core of zeros. The predicates and represent the condition that r must be greater than the minimum even (4) or odd (3) dimension size that can represent an outer boundary and inner core.
When the piecewise equation is satisfied for any dimension , the radius is so large that there is no inner core, resulting in an array of ones.

Examples

Basic Examples (5) 

In[1]:=
sa = ResourceFunction["PerimeterMaskArray"][{5, 5}, 1]
Out[1]=
In[2]:=
MatrixForm[sa]
Out[2]=

Create a 10×10 perimeter mask array with border radius 1:

In[3]:=
ResourceFunction["PerimeterMaskArray"][{10, 10}]
Out[3]=
In[4]:=
ArrayPlot[%, ImageSize -> Small]
Out[4]=

Create a 20×40 perimeter mask array with border radius 5:

In[5]:=
ResourceFunction["PerimeterMaskArray"][{20, 40}, 5]
Out[5]=
In[6]:=
ArrayPlot[%, ImageSize -> Small]
Out[6]=

Create a 3D perimeter mask array:

In[7]:=
ResourceFunction["PerimeterMaskArray"][{20, 30, 40}]
Out[7]=
In[8]:=
ArrayPlot3D[%, ImageSize -> Small]
Out[8]=

When r<1, the resulting array will be all zeros:

In[9]:=
ResourceFunction["PerimeterMaskArray"][{10, 10}, 0] // MatrixForm
Out[9]=

When r>=(dimi/2), the resulting array will be all ones:

In[10]:=
ResourceFunction["PerimeterMaskArray"][{10, 10}, 5] // MatrixForm
Out[10]=

Possible Issues (2) 

PerimeterMaskArray will fail if provided non-integer dimensions:

In[11]:=
ResourceFunction["PerimeterMaskArray"][{1.5, 2}]
Out[11]=

PerimeterMaskArray will fail if provided non-positive dimensions:

In[12]:=
ResourceFunction["PerimeterMaskArray"][{-1, 2}]
Out[12]=

Neat Examples (3) 

Use PerimeterMaskArray to set up and run a fixed boundary 1D cellular automaton simulation:

In[13]:=
With[{rule = 30, init = ArrayPad[{1}, 50], r = 1},
 ArrayPlot[NestList[
   With[{mask = ResourceFunction["PerimeterMaskArray"][
        Dimensions[#, r]]}, (mask*#) + ((1 - mask)*
        CellularAutomaton[rule, #])] &,
   init,
   100]]]
Out[13]=

Use PerimeterMaskArray to set up and run a fixed boundary 2D cellular automaton simulation:

In[14]:=
ArrayPlot[Nest[
  With[{mask = ResourceFunction["PerimeterMaskArray"][
       Dimensions[#]]}, (mask*#) + ((1 - mask)*
       CellularAutomaton[{10, {2, 1}, {1, 1}}, #])] &,
  ArrayPad[{{1}}, 50],
  47]]
Out[14]=

Interpret a square perimeter mask array as a graph adjacency matrix:

In[15]:=
AdjacencyGraph[ResourceFunction["PerimeterMaskArray"][{20, 20}, 5]]
Out[15]=

Publisher

Phileas Dazeley-Gaist

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.1 – 13 August 2025
  • 1.0.0 – 11 August 2025

Related Resources

License Information