padarray
Pad array
Description
pads array 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
Add Padding to 2-D and 3-D Arrays
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
A
— Array to be padded
numeric array | logical array | categorical array
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
padsize
— Amount of padding
vector of nonnegative integers
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
padval
— Pad value
0
| numeric scalar | "circular"
| "replicate"
| "symmetric"
| string scalar | character vector | missing
Pad value, specified as one of the following.
Image Type | Format of Fill Values |
---|---|
Numeric image or logical image |
|
Categorical image |
|
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
direction
— Direction to pad array
"both"
(default) | "post"
| "pre"
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
B
— Padded array
numeric array | logical array | categorical array
Padded array, returned as an array of the same data type as
A
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
padarray
supports the generation of 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,
padarray
supports only up to 3-D inputsThe input arguments
padval
anddirection
must be compile-time constants.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Input arrays of data type categorical are not supported.
When generating code,
padarray
supports only up to 3-D inputs.The input arguments
padval
anddirection
must be compile-time constants.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)