Function Reference
— Function File: B = padarray (A,padsize)
— Function File: B = padarray (A,padsize,padval)
— Function File: B = padarray (A,padsize,padval,direction)

Pads an array in a configurable way.

B = padarray(A,padsize) pads an array A with zeros, where padsize defines the amount of padding to add in each dimension (it must be a vector of positive integers).

Each component of padsize defines the number of elements of padding that will be added in the corresponding dimension. For instance, [4,5] adds 4 elements of padding in first dimension (vertical) and 5 in second dimension (horizontal).

B = padarray(A,padsize,padval) pads A using the value specified by padval. padval can be a scalar or a string. Possible values are:

0
Pads with 0 as described above. This is the default behaviour.
scalar
Pads using padval as a padding value.
'circular'
Pads with a circular repetition of elements in A (similar to tiling A).
'replicate'
Pads 'replicating' values of A which are at the border of the array.
'symmetric'
Pads with a mirror reflection of A.

B = padarray(A,padsize,padval,direction) pads A defining the direction of the pad. Possible values are:

'both'
For each dimension it pads before the first element the number of elements defined by padsize and the same number again after the last element. This is the default value.
'pre'
For each dimension it pads before the first element the number of elements defined by padsize.
'post'
For each dimension it pads after the last element the number of elements defined by padsize.

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