---
title: "ArrayMesh"
language: "en"
type: "Symbol"
summary: "ArrayMesh[array] generates a mesh region from an array of rank d in which each cell has a geometric dimension d and represents a nonzero value of the array."
keywords: 
- matrix mesh
- discrete data mesh
- ragged array mesh
- grid mesh
- cartesian grid mesh
- regular grid mesh
canonical_url: "https://reference.wolfram.com/language/ref/ArrayMesh.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Mesh-Based Geometric Regions"
    link: "https://reference.wolfram.com/language/guide/MeshRegions.en.md"
  - 
    title: "Geometric Computation"
    link: "https://reference.wolfram.com/language/guide/GeometricComputation.en.md"
related_functions: 
  - 
    title: "ArrayPlot"
    link: "https://reference.wolfram.com/language/ref/ArrayPlot.en.md"
  - 
    title: "MatrixPlot"
    link: "https://reference.wolfram.com/language/ref/MatrixPlot.en.md"
  - 
    title: "ArrayFlatten"
    link: "https://reference.wolfram.com/language/ref/ArrayFlatten.en.md"
  - 
    title: "MeshRegion"
    link: "https://reference.wolfram.com/language/ref/MeshRegion.en.md"
  - 
    title: "BoundaryMeshRegion"
    link: "https://reference.wolfram.com/language/ref/BoundaryMeshRegion.en.md"
  - 
    title: "CellularAutomaton"
    link: "https://reference.wolfram.com/language/ref/CellularAutomaton.en.md"
---
# ArrayMesh

ArrayMesh[array] generates a mesh region from an array of rank d in which each cell has a geometric dimension d and represents a nonzero value of the array.

## Details and Options

* ``ArrayMesh`` is generated from a grid where cells are intervals, squares, or cubes, and grid points are uniformly spaced integer points.

* ``ArrayMesh`` arranges successive rows of ``array`` down and successive columns across.

[image]

* ``ArrayMesh`` has the same options as ``MeshRegion``, with the following additions:

|               |           |                                           |
| ------------- | --------- | ----------------------------------------- |
| DataRange     | Automatic | the range of mesh coordinates to generate |
| DataReversed  | False     | whether to reverse the order of rows      |

* ``ArrayMesh`` works with ``SparseArray`` objects.

## Examples (40)

### Basic Examples (3)

A 1D array mesh:

```wl
In[1]:= ArrayMesh[{1, 0, 1, 1, 0, 1}]

Out[1]= [image]
```

---

A 2D array mesh:

```wl
In[1]:= ArrayMesh[{{1, 1, 0, 1}, {1, 1, 1, 1}, {0, 1, 0, 1}}]

Out[1]= [image]
```

---

A 3D array mesh:

```wl
In[1]:= ArrayMesh[{{{1, 1}, {1, 1}, {1, 0}}, {{0, 1}, {1, 1}, {0, 0}}}]

Out[1]= [image]
```

### Scope (2)

Create a 1D array mesh:

```wl
In[1]:= ArrayMesh[{1, 1, 0, 0, 1}]

Out[1]= [image]
```

2D array mesh:

```wl
In[2]:= ArrayMesh[{{1, 1, 0, 1}, {1, 1, 1, 1}, {0, 1, 0, 1}}]

Out[2]= [image]
```

3D array mesh:

```wl
In[3]:= ArrayMesh[{{{1, 1}, {1, 1}, {1, 0}}, {{0, 1}, {1, 1}, {0, 0}}}]

Out[3]= [image]
```

---

``ArrayMesh`` works on ``SparseArray`` :

```wl
In[1]:= ArrayMesh[SparseArray[{{x_, y_} /; Abs[x - y] < 2 -> 1}, {3, 3}]]

Out[1]= [image]
```

### Options (14)

#### DataRange (1)

``DataRange`` allows you to specify the range of mesh coordinates to generate :

```wl
In[1]:= ArrayMesh[{{1, 0}, {1, 1}}]

Out[1]= [image]

In[2]:= MeshCoordinates[%]

Out[2]= {{0., 0.}, {0., 1.}, {1., 0.}, {1., 1.}, {0., 2.}, {1., 2.}, {2., 0.}, {2., 1.}}
```

Specify a different range:

```wl
In[3]:= ArrayMesh[{{1, 0}, {1, 1}}, DataRange -> {{0, 1}, {0, 2}}]

Out[3]= [image]

In[4]:= MeshCoordinates[%]

Out[4]= {{0., 0.}, {0., 1.}, {0.5, 0.}, {0.5, 1.}, {0., 2.}, {0.5, 2.}, {1., 0.}, {1., 1.}}
```

#### DataReversed (1)

``DataReversed`` allows you to reverse the order of rows :

```wl
In[1]:= ArrayMesh[{{1, 0, 0}, {1, 1, 0}, {1, 0, 1}, {1, 1, 1}}]

Out[1]= [image]
```

Reverse the order of rows:

```wl
In[2]:= ArrayMesh[{{1, 0, 0}, {1, 1, 0}, {1, 0, 1}, {1, 1, 1}}, DataReversed -> True]

Out[2]= [image]
```

#### MeshCellHighlight (2)

``MeshCellHighlight`` allows you to specify highlighting for parts of an ``ArrayMesh`` :

```wl
In[1]:= ArrayMesh[{{1, 0}, {1, 1}}, MeshCellHighlight -> {{1, All} -> Red, {0, All} -> Black}]

Out[1]= [image]
```

---

Individual cells can be highlighted using their cell index:

```wl
In[1]:= ArrayMesh[{{1, 0}, {1, 1}}, MeshCellHighlight -> {{1, 1} -> {Thick, Red}, {1, 2} -> {Dashed, Black}}]

Out[1]= [image]
```

Or by the cell itself:

```wl
In[2]:= ArrayMesh[{{1, 0}, {1, 1}}, MeshCellHighlight -> {Line[{4, 6}] -> {Thick, Red}, Line[{5, 6}] -> {Dashed, Black}}]

Out[2]= [image]
```

#### MeshCellLabel (2)

``MeshCellLabel`` can be used to label parts of an ``ArrayMesh`` :

```wl
In[1]:= ArrayMesh[{{1, 0}, {1, 1}}, MeshCellLabel -> {0 -> "Index"}]

Out[1]= [image]
```

---

Individual cells can be labeled using their cell index:

```wl
In[1]:= ArrayMesh[{{1, 0}, {1, 1}}, MeshCellLabel -> {{1, 2} -> "x", {1, 1} -> "y"}]

Out[1]= [image]
```

Or by the cell itself:

```wl
In[2]:= ArrayMesh[{{1, 0}, {1, 1}}, MeshCellLabel -> {Line[{5, 6}] -> "x", Line[{4, 6}] -> "y"}]

Out[2]= [image]
```

#### MeshCellMarker (1)

``MeshCellMarker`` can be used to assign values to parts of an ``ArrayMesh`` :

```wl
In[1]:= ArrayMesh[{{1, 0}}, MeshCellMarker -> {{0, 1} -> 1, {0, 2} -> 2, {0, 3} -> 3, {0, 4} -> 4}]

Out[1]= [image]
```

Use ``MeshCellLabel`` to show the markers:

```wl
In[2]:= ArrayMesh[{{1, 0}}, MeshCellMarker -> {{0, 1} -> 1, {0, 2} -> 2, {0, 3} -> 3, {0, 4} -> 4}, MeshCellLabel -> {0 -> "Marker"}]

Out[2]= [image]
```

#### MeshCellShapeFunction (2)

``MeshCellShapeFunction`` can be used to assign values to parts of an ``ArrayMesh`` :

```wl
In[1]:= ArrayMesh[{{{1, 0}}}, MeshCellShapeFunction -> {1 -> (Tube[#1, .1]&)}]

Out[1]= [image]
```

---

Individual cells can be drawn using their cell index:

```wl
In[1]:= ArrayMesh[{{{1, 0}}}, MeshCellShapeFunction -> {{1, 4} -> (Tube[#1, .1]&)}]

Out[1]= [image]
```

Or by the cell itself:

```wl
In[2]:= ArrayMesh[{{{1, 0}}}, MeshCellShapeFunction -> {Line[{1, 2}] -> (Tube[#1, .1]&)}]

Out[2]= [image]
```

#### MeshCellStyle (3)

``MeshCellStyle`` allows you to specify styling for parts of an ``ArrayMesh`` :

```wl
In[1]:= ArrayMesh[{{1, 0}}, MeshCellStyle -> {{1, All} -> Red, {0, All} -> Black}]

Out[1]= [image]
```

---

Individual cells can be highlighted using their cell index:

```wl
In[1]:= ArrayMesh[{{1, 0}}, MeshCellStyle -> {{1, 1} -> {Thick, Red}, {1, 2} -> {Dashed, Black}}]

Out[1]= [image]
```

Or by the cell itself:

```wl
In[2]:= ArrayMesh[{{1, 0}}, MeshCellStyle -> {Line[{3, 4}] -> {Thick, Red}, Line[{2, 4}] -> {Dashed, Black}}]

Out[2]= [image]
```

---

Give explicit color directives to specify colors for individual cells:

```wl
In[1]:= ArrayMesh[{{1, 0, 0, 1}, {1, 1, 0, 1}, {1, 0, 1, 1}}, MeshCellStyle -> {{2, All} -> Black, {2, 1} -> Red, {2, {2, 3}} -> Pink}]

Out[1]= [image]
```

#### PlotTheme (2)

Use a theme with grid lines and a legend:

```wl
In[1]:= ArrayMesh[{{1, 0}, {1, 1}}, PlotTheme -> "Detailed"]

Out[1]= [image]
```

---

Use a theme to draw a wireframe:

```wl
In[1]:= ArrayMesh[{{1, 0}, {1, 1}}, PlotTheme -> "Lines"]

Out[1]= [image]
```

### Applications (15)

#### Cellular Automaton (5)

A two-dimensional cellular automaton evolution:

```wl
In[1]:= ArrayMesh[CellularAutomaton[30, {{1}, 0}, 50]]

Out[1]= [image]
```

---

Show a sequence of steps in the evolution of a 3D cellular automaton:

```wl
In[1]:= ArrayMesh /@ CellularAutomaton[{14, {2, 1}, {1, 1, 1}}, {{{{1}}}, 0}, 10]

Out[1]= [image]
```

---

Use an outer-totalistic 2D cellular automaton to generate a maze-like pattern:

```wl
In[1]:= ArrayMesh[CellularAutomaton[{746, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}}, {{Table[1, {7}]}, 0}, {{{150}}}]]

Out[1]= [image]
```

---

Show a "glider" in the Game of Life:

```wl
In[1]:= c = CellularAutomaton[{224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}}, {{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}}, 0}, 8];

In[2]:= glider = (First /@ Position[Flatten[#], 1])& /@ c;

In[3]:= ArrayMesh[ConstantArray[1, {5, 5}], MeshCellStyle -> {{1, All} -> Black, {2, All} -> White, {2, #} -> Black}]& /@ glider

Out[3]= [image]
```

---

Patterns generated by a sequence of 2D nine-neighbor rules:

```wl
In[1]:= Table[ArrayMesh[CellularAutomaton[{i, {2, 1}, {1, 1}}, {{{1}}, 0}, {{{30}}}]], {i, 2, 20, 4}]

Out[1]= [image]
```

Mean cell values:

```wl
In[2]:= Table[ArrayMesh[Mean[CellularAutomaton[{i, {2, 1}, {1, 1}}, {{{1}}, 0}, 10]]], {i, 2, 20, 4}]

Out[2]= {[image], [image], [image], [image], [image]}
```

#### Image (2)

Convert a 2D image to a ``MeshRegion`` :

```wl
In[1]:= img = [image];
```

Cells and styles:

```wl
In[2]:= cells = ImageData[img];

In[3]:= style = MapIndexed[{2, First[#2]} -> GrayLevel[#1] &, Flatten[cells]];
```

The mesh:

```wl
In[4]:= ArrayMesh[cells, MeshCellStyle -> style]

Out[4]= [image]
```

---

Convert a 3D image:

```wl
In[1]:= img = Image3D[RandomReal[1, {2, 3, 4}], ColorFunction -> GrayLevel]

Out[1]= [image]
```

Cells and styles:

```wl
In[2]:= cells = Transpose[ImageData[img], {2, 3, 1}];

In[3]:= style = MapIndexed[{3, First[#2]} -> GrayLevel[#1] &, Flatten[cells]];
```

The mesh:

```wl
In[4]:= ArrayMesh[cells, MeshCellStyle -> style]

Out[4]= [image]
```

#### Pattern (2)

Generate a simple 2D pattern:

```wl
In[1]:= ArrayMesh[Table[If[Mod[i, 4] == 2 && Mod[j, 4] == 2, 0, 1], {i, 11}, {j, 11}]]

Out[1]= [image]
```

3D pattern:

```wl
In[2]:=
ArrayMesh[{{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, 
	{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}}]

Out[2]= [image]
```

More involved patterns:

```wl
In[3]:= ArrayMesh[CellularAutomaton[{14, {2, 1}, {1, 1}}, {{{1}}, 0}, 10]]

Out[3]= [image]
```

---

Construct a Seidel mesh, i.e. a mesh region with tunnels going in every direction without crossing:

```wl
In[1]:= SeidelMesh[{r_, s_, t_}] := ArrayMesh[Table[If[Mod[i, 4] == 2 && Mod[j, 4] == 2 || Mod[i, 4] == 0 && Mod[k, 4] == 0 || Mod[j, 4] == 0 && Mod[k, 4] == 2, 0, 1], {i, 3 + r 4}, {j, 3 + s 4}, {k, 3 + t 4}]]

In[2]:= SeidelMesh[{1, 1, 1}]

Out[2]= [image]
```

By converting to a boundary mesh and styling it, it becomes easier to comprehend:

```wl
In[3]:= HighlightMesh[BoundaryMesh[%], {Style[1, None], Style[2, Opacity[0.5]]}]

Out[3]= [image]
```

#### SubstitutionSystem (4)

A 1D Cantor mesh:

```wl
In[1]:= ArrayMesh[{1, 0, 1, 0, 0, 0, 1, 0, 1}]

Out[1]= [image]
```

Length of the Cantor set at each stage:

```wl
In[2]:= Table[Rationalize[RegionMeasure[ArrayMesh[n]]], {n, SubstitutionSystem[{1 -> {1, 0, 1}, 0 -> {0, 0, 0}}, {1}, 5]}]

Out[2]= {1, 2, 4, 8, 16, 32}
```

The formula:

```wl
In[3]:= FindSequenceFunction[%, n]

Out[3]= 2^-1 + n
```

---

Steps in constructing a Cantor set:

```wl
In[1]:= ArrayMesh[{#}]& /@ SubstitutionSystem[{1 -> {1, 0, 1}, 0 -> {0, 0, 0}}, {1}, 3]

Out[1]= {[image], [image], [image], [image]}
```

---

Create an analogous 2D nested object:

```wl
In[1]:= ArrayMesh /@ SubstitutionSystem[{1 -> {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, 0 -> ConstantArray[0, {3, 3}]}, {{1}}, 3]

Out[1]= {[image], [image], [image], [image]}
```

---

3D Menger sponge:

```wl
In[1]:= ArrayMesh /@ SubstitutionSystem[{1 -> 1 - CrossMatrix[{1, 1, 1}], 0 -> ConstantArray[0, {3, 3, 3}]}, {{{1}}}, 3]

Out[1]= [image]
```

#### Game Design (2)

Build a 2D chessboard:

```wl
In[1]:= m = Join@@ConstantArray[{{0, 1, 0, 1, 0, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 1, 0}}, 4];

In[2]:= p = First /@ Position[Flatten[m], 1];

In[3]:= style = {{1, All} -> {Thick, Black}, {2, All} -> White, {2, #}& /@ p -> Black};

In[4]:= r = ArrayMesh[ConstantArray[1, {8, 8}], MeshCellStyle -> style]

Out[4]= [image]
```

3D chessboard:

```wl
In[5]:= m = Join@@ConstantArray[{{{0}, {1}, {0}, {1}, {0}, {1}, {0}, {1}}, {{1}, {0}, {1}, {0}, {1}, {0}, {1}, {0}}}, 4];

In[6]:= p = First /@ Position[Flatten[m], 1];

In[7]:= style = {{1, All} -> {Thick, Black}, {3, All} -> White, {3, #}& /@ p -> Black};

In[8]:= r = ArrayMesh[ConstantArray[1, {8, 8, 1}], MeshCellStyle -> style]

Out[8]= [image]
```

---

Generate tetrominoes, shapes composed of four squares each:

```wl
In[1]:=
reg = ArrayMesh /@ {(⁠|   |   |   |   |
| - | - | - | - |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 |⁠), (⁠|   |   |   |   |
| - | - | - | - |
| 1 | 1 | 0 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 0 |⁠), (⁠|   |   |   |   |
| - | - | - | - |
| 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 |⁠), (⁠|   |   |   |   |
| - | - | - | - |
| 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 0 |⁠), (⁠|   |   |   |   |
| - | - | - | - |
| 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 0 |⁠), (⁠|   |   |   |   |
| - | - | - | - |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 |
| 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 |⁠), (⁠|   |   |   |   |
| - | - | - | - |
| 1 | 1 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 |⁠)}

Out[1]= {[image], [image], [image], [image], [image], [image], [image]}
```

Color tetrominoes:

```wl
In[2]:= col = ColorData[97, "ColorList"];

In[3]:= Table[MeshRegion[reg[[i]], MeshCellStyle -> {2 -> col[[i]]}], {i, 7}]

Out[3]= [image]
```

### Properties & Relations (6)

The output of ``ArrayMesh`` is always a full-dimensional ``MeshRegion`` :

```wl
In[1]:= ArrayMesh[RandomInteger[1, {3, 4}]]

Out[1]= [image]

In[2]:= {MeshRegionQ[%], RegionDimension[%]}

Out[2]= {True, 2}
```

---

``ArrayMesh`` consists of intervals in 1D:

```wl
In[1]:= ArrayMesh[{1, 1, 0, 0, 1}]

Out[1]= [image]

In[2]:= MeshCells[%, RegionDimension[%]]

Out[2]= {Line[{1, 2}], Line[{2, 3}], Line[{4, 5}]}
```

Rectangles in 2D:

```wl
In[3]:= ArrayMesh[{{0, 0, 1, 1}, {0, 1, 1, 0}, {1, 0, 0, 0}}]

Out[3]= [image]

In[4]:= MeshCells[%, RegionDimension[%]]

Out[4]= {Polygon[{9, 11, 10, 7}], Polygon[{12, 13, 11, 9}], Polygon[{6, 7, 5, 4}], Polygon[{8, 9, 7, 6}], Polygon[{3, 4, 2, 1}]}
```

Hexahedrons in 3D:

```wl
In[5]:= ArrayMesh[{{{1, 0}, {0, 1}}}]

Out[5]= [image]

In[6]:= MeshCells[%, RegionDimension[%]]

Out[6]= {Hexahedron[{1, 3, 4, 2, 5, 7, 8, 6}], Hexahedron[{9, 10, 11, 3, 12, 13, 14, 7}]}
```

---

``ArrayPlot`` can be used to generate a plot:

```wl
In[1]:= arr = {{0, 1, 1, 0}, {1, 0, 1, 0}, {1, 1, 0, 1}};

In[2]:= ArrayMesh[arr]

Out[2]= [image]
```

Show plot:

```wl
In[3]:= ArrayPlot[arr]

Out[3]= [image]
```

---

``MatrixPlot`` can be used to generate a plot:

```wl
In[1]:= arr = {{0, 1, 1, 0}, {1, 0, 1, 0}, {1, 1, 0, 1}};

In[2]:= ArrayMesh[arr]

Out[2]= [image]
```

Show plot:

```wl
In[3]:= MatrixPlot[arr]

Out[3]= [image]
```

---

Find a boundary mesh region by using ``BoundaryMesh`` :

```wl
In[1]:= BoundaryMesh[ArrayMesh[{{0, 1, 1, 0}, {1, 0, 1, 0}, {1, 1, 0, 1}}]]

Out[1]= [image]
```

---

``DataRange``-> range is equivalent to using ``RescalingTransform``[{...}, range]:

```wl
In[1]:= ArrayMesh[{{1, 0}, {1, 1}}, DataRange -> {{1, 2}, {1, 3}}, Frame -> True]

Out[1]= [image]
```

Use ``RescalingTransform`` :

```wl
In[2]:= box  = TransformedRegion[ArrayMesh[{{1, 0}, {1, 1}}], RescalingTransform[{{0, 2}, {0, 2}}, {{1, 2}, {1, 3}}]];

In[3]:= MeshRegion[box, Frame -> True]

Out[3]= [image]
```

## See Also

* [`ArrayPlot`](https://reference.wolfram.com/language/ref/ArrayPlot.en.md)
* [`MatrixPlot`](https://reference.wolfram.com/language/ref/MatrixPlot.en.md)
* [`ArrayFlatten`](https://reference.wolfram.com/language/ref/ArrayFlatten.en.md)
* [`MeshRegion`](https://reference.wolfram.com/language/ref/MeshRegion.en.md)
* [`BoundaryMeshRegion`](https://reference.wolfram.com/language/ref/BoundaryMeshRegion.en.md)
* [`CellularAutomaton`](https://reference.wolfram.com/language/ref/CellularAutomaton.en.md)

## Related Guides

* [Mesh-Based Geometric Regions](https://reference.wolfram.com/language/guide/MeshRegions.en.md)
* [Geometric Computation](https://reference.wolfram.com/language/guide/GeometricComputation.en.md)

## History

* [Introduced in 2016 (10.4)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn104.en.md)