Function File: padarray (A, padsize)
Function File: padarray (…, padval)
Function File: padarray (…, pattern)
Function File: padarray (…, direction)

Pad array or matrix.

Adds padding of length padsize, to a numeric matrix A. padsize must be a vector of non-negative values, each of them defining the length of padding to its corresponding dimension. For example, if padsize is [4 5], it adds 4 rows (1st dimension) and 5 columns (2nd dimension), to both the start and end of A.

If there’s less values in padsize than number of dimensions in A, they’re assumed to be zero. Singleton dimensions of A are also padded accordingly (except when pattern is "reflect").

The values used in the padding can either be a scalar value padval, or the name of a specific pattern. Available patterns are:

"zeros" (default)

Pads with the value 0 (same as passing a padval of 0). This is the default.

"circular"

Pads with a circular repetition of elements in A (similar to tiling A).

"replicate"

Pads replicating the values at the border of A.

"symmetric"

Pads with a mirror reflection of A.

"reflect"

Same as "symmetric", but the borders are not used in the padding. Because of this, it is not possible to pad singleton dimensions.

By default, padding is done in both directions. To change this, direction can be one of the following values:

"both" (default)

Pad each dimension before the first element of A the number of elements defined by padsize, and the same number again after the last element. This is the default.

"pre"

Pad each dimension before the first element of A the number of elements defined by padsize.

"post"

Pad each dimension after the last element of A the number of elements defined by padsize.

See also: cat, flip, resize, prepad, postpad.

Demonstration 1

The following code

 padarray([1,2,3;4,5,6],[2,1])
 % pads [1,2,3;4,5,6] with a whole border of 2 rows and 1 columns of 0

Produces the following output

ans =

   0   0   0   0   0
   0   0   0   0   0
   0   1   2   3   0
   0   4   5   6   0
   0   0   0   0   0
   0   0   0   0   0

Demonstration 2

The following code

 padarray([1,2,3;4,5,6],[2,1],5)
 % pads [1,2,3;4,5,6] with a whole border of 2 rows and 1 columns of 5

Produces the following output

ans =

   5   5   5   5   5
   5   5   5   5   5
   5   1   2   3   5
   5   4   5   6   5
   5   5   5   5   5
   5   5   5   5   5

Demonstration 3

The following code

 padarray([1,2,3;4,5,6],[2,1],0,'pre')
 % pads [1,2,3;4,5,6] with a left and top border of 2 rows and 1 columns of 0

Produces the following output

ans =

   0   0   0   0
   0   0   0   0
   0   1   2   3
   0   4   5   6

Demonstration 4

The following code

 padarray([1,2,3;4,5,6],[2,1],'circular')
 % pads [1,2,3;4,5,6] with a whole 'circular' border of 2 rows and 1 columns
 % border 'repeats' data as if we tiled blocks of data

Produces the following output

ans =

   3   1   2   3   1
   6   4   5   6   4
   3   1   2   3   1
   6   4   5   6   4
   3   1   2   3   1
   6   4   5   6   4

Demonstration 5

The following code

 padarray([1,2,3;4,5,6],[2,1],'replicate')
 % pads [1,2,3;4,5,6] with a whole border of 2 rows and 1 columns which
 % 'replicates' edge data

Produces the following output

ans =

   1   1   2   3   3
   1   1   2   3   3
   1   1   2   3   3
   4   4   5   6   6
   4   4   5   6   6
   4   4   5   6   6

Demonstration 6

The following code

 padarray([1,2,3;4,5,6],[2,1],'symmetric')
 % pads [1,2,3;4,5,6] with a whole border of 2 rows and 1 columns which
 % is symmetric to the data on the edge 

Produces the following output

ans =

   4   4   5   6   6
   1   1   2   3   3
   1   1   2   3   3
   4   4   5   6   6
   4   4   5   6   6
   1   1   2   3   3

Package: image