Change value of boundary of a multidimensional array

How can we change the value of boundary of a multidimensional Array with arbitrary rank. I tried to extract the indicies with:

dim = {8, 16, 32, 64};
rank = Length[dim];
arr = Array[Subscript[m, ##] &, dim];
allIndicies = CoordinateBoundingBoxArray[{Table[1, rank], dim}];
idx = Select[Flatten[allIndicies, rank - 1], MemberQ[#, 1] || ContainsAny[# - dim, {0}] &];


But I wasn't able to find a way to use those indices to set a value on the border of my array:

arr[[idx]] = 0;


And I don't think my way to extract indicies is very efficient. I don't think this way is optimal anyway.

• assuming idx is the correct list of indices, you can use arr2=ReplacePart[arr,idx->0] or arr3 =MapAt[0&, arr, idx].
– kglr
Sep 21, 2018 at 22:20

dim = {4, 5, 3, 2};
arr = Array[Subscript[m, ##] &, dim];
MatrixForm @ arr


borders = Join[#, RotateRight /@ #] &@Thread[{{1, -1}, All, {1, -1}, All}];

m1 = MapAt[ 0 &, arr, borders];
m2 = ReplacePart[ arr, (borders /. All -> Blank[]) -> 0];
m1 == m2


True

MatrixForm @ m1


Alternatively, use ArrayPad on flattened arr and reshape using Partition:

m3 = Partition[ArrayPad[ArrayPad[ArrayFlatten[arr], -1], 1], dim[[3 ;;]]];
m1 == m2 == m3


True

Your question is a bit unclear, but perhaps you can use ArrayPad:

array = Array[m, {3,3,3,3}];


$$\left( \begin{array}{ccc} \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) \\ \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & m(2,2,2,2) & 0 \\ 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) \\ \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) \\ \end{array} \right)$$

or:

array = Array[m, {4,4,4,4}];

$$\left( \begin{array}{cccc} \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) \\ \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & m(2,2,2,2) & m(2,2,2,3) & 0 \\ 0 & m(2,2,3,2) & m(2,2,3,3) & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & m(2,3,2,2) & m(2,3,2,3) & 0 \\ 0 & m(2,3,3,2) & m(2,3,3,3) & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) \\ \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & m(3,2,2,2) & m(3,2,2,3) & 0 \\ 0 & m(3,2,3,2) & m(3,2,3,3) & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & m(3,3,2,2) & m(3,3,2,3) & 0 \\ 0 & m(3,3,3,2) & m(3,3,3,3) & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) \\ \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) \\ \end{array} \right)$$