This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

padarray

Syntax

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

Description

example

B = padarray(A,padsize) pads array A with 0s (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.

You optionally can perform the padding operation using a GPU (requires Parallel Computing Toolbox™). For more information, see Image Processing on a GPU.

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

Add padding on all sides of an image.

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

Input Arguments

collapse all

Array to be padded, specified as a numeric or logical array of any dimension. When padding with a constant value, A must be numeric or logical. When padding using the 'circular', 'replicate', or 'symmetric' methods, A can be of any class.

To perform the padding operation using a GPU, specify A as a gpuArray that contains a numeric or logical array.

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

Amount of padding to add to each dimension, specified as a vector of nonnegative integers. For example, a padsize value of [2 3] means add two elements of padding along the first dimension and three 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.

Data Types: double

Value to use for the padding, specified as one of the following.

Value

Meaning

numeric scalar

Pad array with elements of constant value.

'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.

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

Where 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 a numeric array of the same class as A.

If the padding operation is performed using a GPU, then B is returned as a gpuArray containing a numeric array.

Extended Capabilities

Introduced before R2006a