Documentation

## Syntax

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

## Description

example

````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.You optionally can perform the padding operation using a GPU (requires Parallel Computing Toolbox™).```
````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

`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 ```

`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

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.

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