Main Content

padarray

Description

example

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.

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 one of the following.

Image Type

Format of Fill Values

Numeric image or logical image
  • Numeric scalar — Pad array with elements of constant value. The default pad value of numeric and logical images is 0.

  • "circular" — Pad with circular repetition of elements within the dimension.

  • "replicate" — Pad by repeating border elements of array.

  • "symmetric" — Pad with mirror reflections of the array along the border.

Categorical image
  • Valid category in the image, specified as a string scalar or character vector.

  • missing, which corresponds to the <undefined> category. missing is the default pad value for categorical images. For more information, see missing.

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

Version History

Introduced before R2006a