({{, (RefLink[CenterArray, paclet:ref/CenterArray])[a,n] 
creates a list of length n with the elements of a at the center and 0s elsewhere.}, {, (RefLink[CenterArray, paclet:ref/CenterArray])[a,{(Subscript[n, 1]),(Subscript[n, 2]),…}]
creates an (Subscript[n, 1])×(Subscript[n, 2])×… array with the array a at the center and 0s elsewhere.}, {, (RefLink[CenterArray, paclet:ref/CenterArray])[a,nspec,pad]
uses pad instead of 0 for the background.}, {, (RefLink[CenterArray, paclet:ref/CenterArray])[nspec]
creates an array with a single 1 at the center and 0s elsewhere.}})

OpenersNotesSection(Spacer1)Details

The input a can be a list, full array, ragged array, or any form of array, such as a (RefLink[SparseArray, paclet:ref/SparseArray]) object.

(RefLink[CenterArray, paclet:ref/CenterArray]) always generates a full array.

(RefLink[CenterArray, paclet:ref/CenterArray])[a,nspec,pad,(RefLink[SparseArray, paclet:ref/SparseArray])] creates a (RefLink[SparseArray, paclet:ref/SparseArray]) object.

(RefLink[CenterArray, paclet:ref/CenterArray])[expr,…] is equivalent to (RefLink[CenterArray, paclet:ref/CenterArray])[{expr},…] if expr is not a list.

(RefLink[CenterArray, paclet:ref/CenterArray])[a,n] attempts if possible to put equal padding on both sides of a. If this is not possible, additional padding is placed on the right.

In the form (RefLink[CenterArray, paclet:ref/CenterArray])[a,{(Subscript[n, 1]),(Subscript[n, 2]),…}], any of the (Subscript[n, i]) can be (RefLink[Inherited, paclet:ref/Inherited]), in which case it is assumed to be the corresponding dimension of a, and no padding is added at this level.

If the resulting array has a higher rank, a is centered at the deepest level.

For possible settings of padding value pad, see the reference page for (RefLink[ArrayPad, paclet:ref/ArrayPad]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (14)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Create a list of length 5 with a single 1 at the center:

CenterArray[5]

Create a list of length 5 with the specified element at the center:

CenterArray[x,5]

Place an element at the center of a 2D array:

CenterArray[x,{5,5}]//MatrixForm

OpenersExampleSection3(Spacer1)Applications  (6)

Crop the center of an array:

CenterArray[(({{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}})),{2,3}]

Conform a list of arrays so they have commensurate dimensions:

list={RandomReal[1,{10,1}],RandomReal[1,{20,3}],RandomReal[1,{5,2}],RandomReal[1,{15,2}]};

c=CenterArray[list,Inherited];

Dimensions[c]

Conform elements of a list so they are all of length 3:

list={{9},{4,5},{5,5,3}};

c=CenterArray[list,{Inherited,3}]

Apply a dimension-sensitive operator to the conformed elements:

SquaredEuclideanDistance[#,{1,-1,0}]&/@c

Make a full array from a ragged list of full arrays:

CenterArray[{{{1,2,3},{4,5,6}},{{a,b},{c,d},{e,f}}},{All,All,All}]

Create a sharpening kernel from a lowpass kernel:

lowpasskern=GaussianMatrix[3];

sharpeningkern=CenterArray[2,Dimensions@lowpasskern]-lowpasskern;

Sharpen an image by convolving with the computed kernel:

ImageConvolve[,sharpeningkern]

Show a collection of graphs with missing graphics:

t={{,,},{},{,}};

CenterArray[t,{3,3},Text[Style["?",Red,50]]]//GraphicsGrid

By default, (RefLink[GraphicsGrid, paclet:ref/GraphicsGrid]) has empty spaces at the ends:

t//GraphicsGrid

OpenersExampleSection4(Spacer1)Properties & Relations  (3)

If the amount of padding is an odd number, additional padding is placed on the right:

CenterArray[{1,2},7]

2D input:

CenterArray[{{1,2},{3,4},{5,6}},{6,3}]

The background value of sparse arrays is preserved, irrespective of the specified padding value:

sa=SparseArray[2->5,4,x]

Normal[sa]

CenterArray[sa,8,0]

Normal[%]

The precision of the result is the minimum of the precisions of the input array and the padding value:

a={1.,2.};
pad=0;

Precision/@{a,pad}

CenterArray[a,4,pad]

Precision[%]

OpenersExampleSection5(Spacer1)Possible Issues  (2)

Padding with nonconstant values may not be possible with empty list inputs:

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

Use constant padding instead:

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

This ragged input cannot be centered because it is not a list of full arrays with the same depths:

x={{1,2},{{3}}};

Depth/@x

CenterArray[x,{4,4,2}]

Use a specific target depth instead:

n=Max[Depth/@x];
x2=Table[CenterArray[a,ConstantArray[Inherited,n]],{a,x}]

Center the resulting array in an array of rank 3:

CenterArray[x2,{4,4,2}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ArrayPad, paclet:ref/ArrayPad])  ▪  (RefLink[PadLeft, paclet:ref/PadLeft])  ▪  (RefLink[PadRight, paclet:ref/PadRight])  ▪  (RefLink[BoxMatrix, paclet:ref/BoxMatrix])  ▪  (RefLink[ArrayReshape, paclet:ref/ArrayReshape])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Constructing Lists, paclet:guide/ConstructingLists])}, {▪(RefLinkPlain[Structure Matrices & Convolution Kernels, paclet:guide/StructureMatricesAndConvolutionKernels])}, {▪(RefLinkPlain[Constructing Matrices, paclet:guide/ConstructingMatrices])}})}})