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.
CenterArray
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 allBasic 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}]//MatrixFormScope (11)
Data (9)
CenterArray[{1, 2, 3}, {5, 5}]//MatrixFormCenterArray[{1, 2, 3}, {5, 5}, 0, SparseArray]Normal[%]//MatrixFormCenter a matrix to have 3 rows:
CenterArray[{{1, 2, 3}, {4, 5, 6}}, 3]//MatrixFormCenter a matrix so its dimensions are {3,4}:
CenterArray[{{1, 2, 3}, {4, 5, 6}}, {3, 4}]//MatrixFormCenterArray[{{1, 2}, {3}}, 2]//MatrixFormCenterArray[{{1, 2, 3}, SparseArray[2 -> x, 3]}, {3, 4}]//MatrixFormCreate an array with more rows and fewer columns:
CenterArray[{{1, 2, 3}, {4, 5, 6}}, {4, 1}]//MatrixFormCenterArray[SparseArray[{2, 2} -> x, {2, 3}, 9], {3, 5}]Normal[%]//MatrixFormCenterArray[{{1, 2, 3}}, 5, 0, SparseArray]Normal[%]//MatrixFormOutput Dimensions (1)
Applications (6)
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}]& /@ cMake 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[[image], sharpeningkern]Show a collection of graphs with missing graphics:
t = {{[image], [image], [image]}, {[image]}, {[image], [image]}};CenterArray[t, {3, 3}, Text[Style["?", Red, 50]]]//GraphicsGridBy default, GraphicsGrid has empty spaces at the ends:
t//GraphicsGridProperties & Relations (3)
If the amount of padding is an odd number, additional padding is placed on the right:
CenterArray[{1, 2}, 7]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[%]Possible Issues (2)
Padding with nonconstant values may not be possible with empty list inputs:
CenterArray[{{1, 2}, {}}, {3, 3}, "Fixed"]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 /@ xCenterArray[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}]See Also
Related Guides
History
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: 12-June-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: 12-June-2026]}