1
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ assuming idx is the correct list of indices, you can use arr2=ReplacePart[arr,idx->0] or arr3 =MapAt[0&, arr, idx]. $\endgroup$
    – kglr
    Sep 21 '18 at 22:20
2
$\begingroup$
dim = {4, 5, 3, 2};
arr = Array[Subscript[m, ##] &, dim];
MatrixForm @ arr

enter image description here

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

enter image description here

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

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

True

$\endgroup$
1
$\begingroup$

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

array = Array[m, {3,3,3,3}];
ArrayPad[ArrayPad[array, -1], 1]//MatrixForm //TeXForm

$\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}];
ArrayPad[ArrayPad[array, -1], 1]//MatrixForm //TeXForm

$\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)$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.