({{, (RefLink[ArrayPad, paclet:ref/ArrayPad])[array,m] 
gives an array with m 0s of padding on every side. }, {, (RefLink[ArrayPad, paclet:ref/ArrayPad])[array,m,padding]
uses the specified padding.}, {, (RefLink[ArrayPad, paclet:ref/ArrayPad])[array,{m,n},…]
pads with m elements at the beginning and n elements at the end. }, {, (RefLink[ArrayPad, paclet:ref/ArrayPad])[array,{{(Subscript[m, 1]),(Subscript[n, 1])},{(Subscript[m, 2]),(Subscript[n, 2])},…},…]
pads with (Subscript[m, i]), (Subscript[n, i]) elements at level i in array. }})

OpenersNotesSection(Spacer1)Details and Options

The following forms of padding can be specified:

The padding value "Fixed" indicates that the elements added at each corner should be copies of the elements at the corners of the original array.

"Reversed" indicates that the outermost elements in the array should be repeated as the innermost elements in the padding. "Reflected" specifies that these elements should not be repeated.

With the padding value "Extrapolated", the degree of polynomial used is specified by the option (RefLink[InterpolationOrder, paclet:ref/InterpolationOrder]).

(RefLink[ArrayPad, paclet:ref/ArrayPad]) pads full arrays of any depth.

(RefLink[ArrayPad, paclet:ref/ArrayPad]) works with (RefLink[SparseArray, paclet:ref/SparseArray]) objects.

(RefLink[ArrayPad, paclet:ref/ArrayPad])[array,-m] removes m elements from each side of array.

OpenersPrimaryExamplesSection(Spacer1)Examples  (28)

OpenersExampleSection0(Spacer1)Basic Examples  (6)

Pad the edges of a list with 0s:

ArrayPad[{1,2,3},1]

Pad the edges of a matrix:

ArrayPad[{{1,2},{3,4}},2] // MatrixForm

Specify different padding on each side:

ArrayPad[{1,2,3},{2,4}]

Pad with any expression:

ArrayPad[{1,2,3},2,x]

Pad according to a named rule:

ArrayPad[{1,2,3},2,"Fixed"]

Pad by 5 elements on each row and 1 element on each column:

ArrayPad[{{1,2},{3,4}},{{1},{5}}] // MatrixForm

OpenersExampleSection1(Spacer1)Scope  (12)

Pad only on the right:

ArrayPad[{1,2,3},{0,2}]

Remove elements from each edge of an array:

ArrayPad[Range[10],-2]

Pad with extra rows:

ArrayPad[{{1,2},{3,4}}, {{1},{0}}] //MatrixForm

Pad with extra columns:

ArrayPad[{{1,2},{3,4}}, {{0},{1}}] //MatrixForm

Specify different padding for each edge:

ArrayPad[{{1,2},{3,4}},{{-1,2},{1,3}}]//MatrixForm

Pad only the first level of an array:

ArrayPad[{{1,2},{3,4}},{{1}}]

Pad by repeating periodically:

ArrayPad[{a,b,c},4, "Periodic"]

Pad with the reversal of the list:

ArrayPad[{a,b,c},4,"Reversed"]

Pad with the negative of the reversal:

ArrayPad[{a,b,c},4,"ReversedNegation"]

Pad by reflecting about the edge:

ArrayPad[{a,b,c},4,"Reflected"]

Pad antisymmetrically about the value at the edge:

ArrayPad[{a,b,c},3,"ReflectedDifferences"]

Differences[%]

Pad antisymmetrically about the edge of the array with reversed differences:

ArrayPad[{a,b,c},4,"ReversedDifferences"]

The differences are reflected about the edge of the original array:

Differences[%]

Pad using linear extrapolation:

ArrayPad[{a,b,c},3,"Extrapolated"]

Pad using extrapolation of different orders:

Table[ArrayPad[{1,4,9},4,"Extrapolated", InterpolationOrder->o],{o,0,2}] //MatrixForm

Pad using the maximal extrapolation order:

ArrayPad[N[Exp[Range[0,5]]],5, "Extrapolated",InterpolationOrder->∞]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (1)

(RefLink[ArrayPad, paclet:ref/ArrayPad]) works with (RefLink[SparseArray, paclet:ref/SparseArray]) objects:

ArrayPad[SparseArray[{i_,i_}->i,{4,4}],{{0},{4}},"ReversedNegation"]

MatrixForm[%]

OpenersExampleSection3(Spacer1)Options  (3)

OpenersExampleSubsection0(Spacer1)InterpolationOrder  (3)

Specify the order of interpolation for "Extrapolated" padding:

ArrayPad[{1,5,7,8},{0,3},"Extrapolated",InterpolationOrder->2]

By default, linear interpolation is used:

ArrayPad[{1,5,7,8},{0,3},"Extrapolated"]

Use the maximum possible order, in this case order 3:

ArrayPad[{1,5,7,8},{0,3},"Extrapolated",InterpolationOrder->∞]

OpenersExampleSection4(Spacer1)Applications  (1)

Lay out tiles by reflecting about their edges:

ArrayPlot[ArrayPad[IdentityMatrix[4],8,"Reflected"]]

OpenersExampleSection5(Spacer1)Properties & Relations  (2)

(RefLink[PadLeft, paclet:ref/PadLeft])[list,n] and (RefLink[PadRight, paclet:ref/PadRight])[list,n] pad to make a list of length n:

PadLeft[{1,2,3},5]

PadRight[{1,2,3},5]

(RefLink[ArrayPad, paclet:ref/ArrayPad])[list,n] pads with n additional elements on each side:

ArrayPad[{1,2,3},5]

(RefLink[ArrayPad, paclet:ref/ArrayPad])[list,{-n,0}] is effectively (RefLink[Drop, paclet:ref/Drop])[list,n] for a one-dimensional list:

{ArrayPad[{1,2,3,4},{-2,0}],Drop[{1,2,3,4},2]}

(RefLink[ArrayPad, paclet:ref/ArrayPad])[list,{0,-n}] is effectively (RefLink[Drop, paclet:ref/Drop])[list,-n]:

{ArrayPad[{1,2,3,4},{0,-2}],Drop[{1,2,3,4},-2]}

OpenersExampleSection6(Spacer1)Possible Issues  (2)

A single number or pair of numbers is interpreted to apply in each dimension:

ArrayPad[{{1,2,4}},{1,2},"ReflectedDifferences"]

Explicitly specify the padding amount in each dimension:

ArrayPad[{{1,2,4}},{{0,0},{1,2}},"ReflectedDifferences"]

Normally even empty array dimensions are padded:

ArrayPad[{{},{}},1,x]

With padding types that depend on array values, only nonempty array dimensions are padded:

ArrayPad[{{},{}},1,"Fixed"]

OpenersExampleSection7(Spacer1)Neat Examples  (1)

Visualize the different named padding rules:

a=KroneckerProduct[Sqrt[Range[6]],Sqrt[Range[6]]];

MatrixPlot[a]

Table[MatrixPlot[ArrayPad[a,4,p],Epilog->{EdgeForm[{Thick,Red}],Transparent,Rectangle[{4,4},{10,10}]},FrameTicks->None,PlotLabel->p],{p,{"Fixed","Extrapolated","Periodic","Reflected","ReflectedDifferences","Reversed","ReversedDifferences","ReversedNegation"}}]

Table[MatrixPlot[ArrayPad[a,12,p],Epilog->{EdgeForm[{Thick,Red}],Transparent,Rectangle[{12,12},{18,18}]},FrameTicks->None,PlotLabel->p],{p,{"Fixed","Extrapolated","Periodic","Reflected","ReflectedDifferences","Reversed","ReversedDifferences","ReversedNegation"}}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ImagePad, paclet:ref/ImagePad])  ▪  (RefLink[PadLeft, paclet:ref/PadLeft])  ▪  (RefLink[PadRight, paclet:ref/PadRight])  ▪  (RefLink[CenterArray, paclet:ref/CenterArray])  ▪  (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])  ▪  (RefLink[Join, paclet:ref/Join])  ▪  (RefLink[Padding, paclet:ref/Padding])  ▪  (RefLink[ImageCrop, paclet:ref/ImageCrop])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Rearranging & Restructuring Lists, paclet:guide/RearrangingAndRestructuringLists])}, {▪(RefLinkPlain[Constructing Matrices, paclet:guide/ConstructingMatrices])}, {▪(RefLinkPlain[Structure Matrices & Convolution Kernels, paclet:guide/StructureMatricesAndConvolutionKernels])}, {▪(RefLinkPlain[Wavelet Analysis, paclet:guide/Wavelets])}})}})