padarray
Pad array
Description
B = padarray(A,padsize)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.
Examples
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
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 a numeric scalar, string scalar, or character vector.
This table shows the padding options for numeric and logical images. The
                        default pad value of numeric and logical images is
                            0.
Padding Values for Numeric and Logical Images
| Value | Description | Example | 
|---|---|---|
| numeric scalar,
                                             | Input array values outside the bounds of the
                                                array are assigned the value  |  | 
| 
 | Input array values outside the bounds of the array are computed by mirror-reflecting the array across the array border. |  | 
| 
 | Input array values outside the bounds of the array are assumed to equal the nearest array border value. |  | 
| 
 | Input array values outside the bounds of the array are computed by implicitly assuming the input array is periodic. |  | 
This table shows the padding options for categorical images. The
                        default pad value of categorical images is missing.
Padding Values for Categorical Images
| Value | Description | 
|---|---|
| String scalar or character vector | Pad with elements of a specified category. The string or character vector must correspond to a valid category in the image. | 
| missing | Pad with the <undefined>category. For more information, seemissing. | 
| 
 | Input array values outside the bounds of the array are computed by mirror-reflecting the array across the array border. | 
| 
 | Input array values outside the bounds of the array are assumed to equal the nearest array border value. | 
| 
 | Input array values outside the bounds of the array are computed by implicitly assuming the input array is periodic. | 
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 | 
|---|---|
| 
 | Pads before the first element and after the last array element along each dimension. | 
| 
 | Pad after the last array element along each dimension. | 
| 
 | Pad before the first array element along each dimension. | 
Data Types: char | string
Output Arguments
Padded array, returned as an array of the same data type as
                            A.
Extended Capabilities
Usage notes and limitations:
- padarraysupports the generation of C and C++ code (requires MATLAB® Coder™). For more information, see Code Generation for Image Processing.
- Input arrays of data type categorical are not supported. 
- When generating code, - padarraysupports only up to 3-D inputs
- The input arguments - padvaland- directionmust be compile-time constants.
Usage notes and limitations:
- Input arrays of data type categorical are not supported. 
- When generating code, - padarraysupports only up to 3-D inputs.
- The input arguments - padvaland- directionmust be compile-time constants.
This function fully supports GPU arrays. For more information, see Image Processing on a GPU.
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)