Documentation Center

  • Trial Software
  • Product Updates

padarray

Syntax

B = padarray(A,padsize)
B = padarray(A,padsize,padval)
B = padarray(A,padsize,padval,direction)
gpuarrayB = padarray(gpuarrayA,___)

Description

B = padarray(A,padsize) pads array A with 0's (zeros). padsize is a vector of nonnegative integers that specifies both the amount of padding to add and the dimension along which to add it. The value of an element in the vector specifies the amount of padding to add. The order of the element in the vector specifies the dimension along which to add the padding.

For example, a padsize value of [2 3] means add 2 elements of padding along the first dimension and 3 elements of padding along the second dimension. By default, paddarray adds padding before the first element and after the last element along the specified dimension.

B = padarray(A,padsize,padval) pads array A where padval specifies the value to use as the pad value. padarray uses the value 0 (zero) as the default. padval can be a scalar that specifies the pad value directly or one of the following text strings that specifies the method padarray uses to determine the values of the elements added as padding.

Value

Meaning

'circular'

Pad with circular repetition of elements within the dimension.

'replicate'

Pad by repeating border elements of array.

'symmetric'

Pad array with mirror reflections of itself.

B = padarray(A,padsize,padval,direction) pads A in the direction specified by the string direction. direction can be one of the following strings. The default value is enclosed in braces ({}).

Value

Meaning

{'both'}

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

'post'

Pad after the last array element along each dimension.

'pre'

Pad before the first array element along each dimension.

gpuarrayB = padarray(gpuarrayA,___) performs the padding operation on a GPU, where gpuarrayA is a gpuArray object that contains the image A. The return value gpuarrayB is also a gpuArray. This syntax requires the Parallel Computing Toolbox™.

Code Generation

padarray supports the generation of efficient, production-quality C/C++ code from MATLAB. When generating code, padarray supports only up to 3-D inputs, and the input arguments, padval and direction must be compile-time constants. To see a complete list of toolbox functions that support code generation, see List of Supported Functions with Usage Notes.

Class Support

When padding with a constant value, A can be numeric or logical. When padding using the 'circular', 'replicate', or 'symmetric' methods, A can be of any class. B is of the same class as A.

Examples

Example 1

Add three elements of padding to the beginning of a vector. The padding elements, indicated by the gray shading, contain mirror copies of the array elements.

a = [ 1 2 3 4 ];
b = padarray(a,[0 3],'symmetric','pre')
b ==

Example 2

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. The example uses the value of the last array element as the padding value.

A = [1 2; 3 4];
B = padarray(A,[3 2],'replicate','post')
B =

Example 3

Add three elements of padding to the vertical and horizontal dimensions of a three-dimensional array. Use default values for the pad value and direction.

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

     1     2
     3     4

C(:,:,2) =

     5     6
     7     8

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

D(:,:,2) ===

Perform Padding on a GPU

Add padding on all sides of an image.

gcam = gpuArray(imread('cameraman.tif'));
padcam = padarray(gcam,[50 50],'both');
imshow(padcam)

See Also

| |

Was this topic helpful?