Pad array
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.
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
A
— Array to be paddedArray 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 paddingAmount 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 value0
| 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
B
— Padded arrayPadded array, returned as an array of the same data type as
A
.
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 inputs
The input arguments padval
and
direction
must be compile-time
constants.
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
and
direction
must be compile-time
constants.
This function fully supports GPU arrays. For more information, see Image Processing on a GPU.
You have a modified version of this example. Do you want to open this example with your edits?