Main Content

padarray

Description

B = padarray(A,padsize) pads array A with an amount of padding in each dimension specified by padsize. The padarray function pads numeric or logical images with the value 0 and categorical images with the category <undefined>. By default, paddarray adds padding before the first element and after the last element of each dimension.

example

B = padarray(A,padsize,padval) pads array A where padval specifies a constant value to use for padded elements or a method to replicate array elements.

B = padarray(___,direction) pads A in the direction specified by direction.

Examples

collapse all

Pad the Beginning of a Vector

Add three elements of padding to the beginning of a vector with padding value 9.

A = [ 1 2 3 4 ]
A = 1×4

     1     2     3     4

B = padarray(A,3,9,'pre')
B = 4×4

     9     9     9     9
     9     9     9     9
     9     9     9     9
     1     2     3     4

Pad Each Dimension of a 2-D Array

Add three elements of padding to the end of the first dimension of the array and two elements of padding to the end of the second dimension. Use the value of the last array element on each dimension as the padding value.

A = [ 1 2; 3 4 ]
A = 2×2

     1     2
     3     4

B = padarray(A,[3 2],'replicate','post')
B = 5×4

     1     2     2     2
     3     4     4     4
     3     4     4     4
     3     4     4     4
     3     4     4     4

Pad Each Dimension of a 3-D Array

Add three elements of padding to each dimension of a three-dimensional array. Each pad element contains the value 0.

First create the 3-D array.

A = [1 2; 3 4];
B = [5 6; 7 8];
C = cat(3,A,B)
C = 
C(:,:,1) =

     1     2
     3     4


C(:,:,2) =

     5     6
     7     8

Pad the 3-D Array

D = padarray(C,[3 3],0,'both')
D = 
D(:,:,1) =

     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     1     2     0     0     0
     0     0     0     3     4     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0


D(:,:,2) =

     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     5     6     0     0     0
     0     0     0     7     8     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0

Input Arguments

collapse all

Array to be padded, specified as a numeric, logical, or categorical array of any dimension.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | categorical

Amount of padding to add to each dimension, specified as a vector of nonnegative integers. For example, a padsize value of [2 3] adds two elements of padding along the first dimension and three elements of padding along the second dimension.

Data Types: double

Pad value, specified as a numeric scalar, string scalar, or character vector.

This table shows the padding options for numeric and logical images. The default pad value of numeric and logical images is 0.

Padding Values for Numeric and Logical Images

Value

Description

Example

numeric scalar, X

Input array values outside the bounds of the array are assigned the value X. When no padding option is specified, the default is 0.

[314159265][2222222222222222314222215922222652222222222222222]

"symmetric"

Input array values outside the bounds of the array are computed by mirror-reflecting the array across the array border.

[314159265][5115995133144113314415115995622655662265565115995]

"replicate"

Input array values outside the bounds of the array are assumed to equal the nearest array border value.

[314159265][3331444333144433314441115999222655522265552226555]

"circular"

Input array values outside the bounds of the array are computed by implicitly assuming the input array is periodic.

[314159265][5915915652652614314315915915652652614314315915915]

This table shows the padding options for categorical images. The default pad value of categorical images is missing.

Padding Values for Categorical Images

ValueDescription
String scalar or character vectorPad with elements of a specified category. The string or character vector must correspond to a valid category in the image.
missingPad with the <undefined> category. For more information, see missing.

"symmetric"

Input array values outside the bounds of the array are computed by mirror-reflecting the array across the array border.

"replicate"

Input array values outside the bounds of the array are assumed to equal the nearest array border value.

"circular"

Input array values outside the bounds of the array are computed by implicitly assuming the input array is periodic.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string

Direction to pad array along each dimension, specified as one of the following values:

Value

Meaning

"both"

Pads before the first element and after the last array element along each dimension.

"post"

Pad after the last array element along each dimension.

"pre"

Pad before the first array element along each dimension.

Data Types: char | string

Output Arguments

collapse all

Padded array, returned as an array of the same data type as A.

Extended Capabilities

expand all

Version History

Introduced before R2006a