WOLFRAM

Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how
Wolfram Language & System Documentation Center

CenterArray [a,n]

creates a list of length n with the elements of a at the center and zeros elsewhere.

CenterArray [a,{n1,n2,}]

creates an n1×n2× array with the array a at the center and zeros elsewhere.

CenterArray [a,nspec,pad]

uses pad instead of zero for the background.

CenterArray [nspec]

creates an array with a single 1 at the center and zeros elsewhere.

Details
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Data  
Output Dimensions  
Padding  
Applications  
Properties & Relations  
Possible Issues  
See Also
Related Guides
History
Cite this Page

CenterArray [a,n]

creates a list of length n with the elements of a at the center and zeros elsewhere.

CenterArray [a,{n1,n2,}]

creates an n1×n2× array with the array a at the center and zeros elsewhere.

CenterArray [a,nspec,pad]

uses pad instead of zero for the background.

CenterArray [nspec]

creates an array with a single 1 at the center and zeros elsewhere.

Details

  • The input a can be a list, full array, ragged array, or any form of array, such as a SparseArray object.
  • CenterArray always generates a full array.
  • CenterArray [a,nspec,pad,SparseArray ] creates a SparseArray object.
  • CenterArray [expr,] is equivalent to CenterArray [{expr},] if expr is not a list.
  • 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 CenterArray [a,{n1,n2,}], any of the ni can be 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 ArrayPad .

Examples

open all close all

Basic Examples  (3)

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

Wolfram Language code: CenterArray[5]

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

Wolfram Language code: CenterArray[x, 5]

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

Wolfram Language code: CenterArray[x, {5, 5}]//MatrixForm

Scope  (11)

Data  (9)

Center a list in a 2D array:

Wolfram Language code: CenterArray[{1, 2, 3}, {5, 5}]//MatrixForm

Create a sparse array:

Wolfram Language code: CenterArray[{1, 2, 3}, {5, 5}, 0, SparseArray]
Wolfram Language code: Normal[%]//MatrixForm

Center a matrix to have 3 rows:

Wolfram Language code: CenterArray[{{1, 2, 3}, {4, 5, 6}}, 3]//MatrixForm

Center a matrix so its dimensions are {3,4}:

Wolfram Language code: CenterArray[{{1, 2, 3}, {4, 5, 6}}, {3, 4}]//MatrixForm

Center a ragged array:

Wolfram Language code: CenterArray[{{1, 2}, {3}}, 2]//MatrixForm

Center a mixed input:

Wolfram Language code: CenterArray[{{1, 2, 3}, SparseArray[2 -> x, 3]}, {3, 4}]//MatrixForm

Create an array with more rows and fewer columns:

Wolfram Language code: CenterArray[{{1, 2, 3}, {4, 5, 6}}, {4, 1}]//MatrixForm

Center a 2D sparse array:

Wolfram Language code: CenterArray[SparseArray[{2, 2} -> x, {2, 3}, 9], {3, 5}]
Wolfram Language code: Normal[%]//MatrixForm

Create a 2D sparse array:

Wolfram Language code: CenterArray[{{1, 2, 3}}, 5, 0, SparseArray]
Wolfram Language code: Normal[%]//MatrixForm

Output Dimensions  (1)

Return a smaller list:

Wolfram Language code: CenterArray[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 3]

Padding  (1)

The default padding is zero:

Wolfram Language code: CenterArray[{1, 2, 3}, 6]

Specify a different padding:

Wolfram Language code: CenterArray[{1, 2, 3}, 6, Missing[]]

Use "Extrapolated" padding instead:

Wolfram Language code: CenterArray[{1, 2, 3}, 6, "Extrapolated"]

Applications  (6)

Crop the center of an array:

Wolfram Language code: 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:

Wolfram Language code: list = {RandomReal[1, {10, 1}], RandomReal[1, {20, 3}], RandomReal[1, {5, 2}], RandomReal[1, {15, 2}]};
Wolfram Language code: c = CenterArray[list, Inherited];
Wolfram Language code: Dimensions[c]

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

Wolfram Language code: list = {{9}, {4, 5}, {5, 5, 3}};
Wolfram Language code: c = CenterArray[list, {Inherited, 3}]

Apply a dimension-sensitive operator to the conformed elements:

Wolfram Language code: SquaredEuclideanDistance[#, {1, -1, 0}]& /@ c

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

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

Create a sharpening kernel from a lowpass kernel:

Wolfram Language code: lowpasskern = GaussianMatrix[3];
Wolfram Language code: sharpeningkern = CenterArray[2, Dimensions@lowpasskern] - lowpasskern;

Sharpen an image by convolving with the computed kernel:

Wolfram Language code: ImageConvolve[[image], sharpeningkern]

Show a collection of graphs with missing graphics:

Wolfram Language code: t = {{[image], [image], [image]}, {[image]}, {[image], [image]}};
Wolfram Language code: CenterArray[t, {3, 3}, Text[Style["?", Red, 50]]]//GraphicsGrid

By default, GraphicsGrid has empty spaces at the ends:

Wolfram Language code: t//GraphicsGrid

Properties & Relations  (3)

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

Wolfram Language code: CenterArray[{1, 2}, 7]

2D input:

Wolfram Language code: CenterArray[{{1, 2}, {3, 4}, {5, 6}}, {6, 3}]

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

Wolfram Language code: sa = SparseArray[2 -> 5, 4, x]
Wolfram Language code: Normal[sa]
Wolfram Language code: CenterArray[sa, 8, 0]
Wolfram Language code: Normal[%]

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

Wolfram Language code: a = {1., 2.}; pad = 0;
Wolfram Language code: Precision /@ {a, pad}
Wolfram Language code: CenterArray[a, 4, pad]
Wolfram Language code: Precision[%]

Possible Issues  (2)

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

Wolfram Language code: CenterArray[{{1, 2}, {}}, {3, 3}, "Fixed"]

Use constant padding instead:

Wolfram Language code: 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:

Wolfram Language code: x = {{1, 2}, {{3}}};
Wolfram Language code: Depth /@ x
Wolfram Language code: CenterArray[x, {4, 4, 2}]

Use a specific target depth instead:

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

Center the resulting array in an array of rank 3:

Wolfram Language code: CenterArray[x2, {4, 4, 2}]
Wolfram Research (2016), CenterArray, Wolfram Language function, https://reference.wolfram.com/language/ref/CenterArray.html.

Text

Wolfram Research (2016), CenterArray, Wolfram Language function, https://reference.wolfram.com/language/ref/CenterArray.html.

CMS

Wolfram Language. 2016. "CenterArray." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/CenterArray.html.

APA

Wolfram Language. (2016). CenterArray. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CenterArray.html

BibTeX

@misc{reference.wolfram_2026_centerarray, author="Wolfram Research", title="{CenterArray}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/CenterArray.html}", note=[Accessed: 22-August-2026]}

BibLaTeX

@online{reference.wolfram_2026_centerarray, organization={Wolfram Research}, title={CenterArray}, year={2016}, url={https://reference.wolfram.com/language/ref/CenterArray.html}, note=[Accessed: 22-August-2026]}

Top [フレーム]

AltStyle によって変換されたページ (->オリジナル) /