pub fn pad<T>(
array: &Array<T>,
pad_width: &[(usize, usize)],
mode: &str,
constant_values: Option<T>,
) -> Result<Array<T>>Expand description
Pad an array
§Parameters
array- Array to be paddedpad_width- Number of values padded to the edges of each axis. For each axis, provide (before, after) padding sizes.mode- Padding mode:- “constant”: Pads with a constant value (default 0)
- “edge”: Pads with the edge values of array
- “reflect”: Pads with reflection of array mirrored on the first and last values of the axis
- “symmetric”: Pads with reflection of array mirrored along the edge of the array
- “wrap”: Pads with the wrap of the vector along the axis
constant_values- Used in ‘constant’ mode. The values to set the padded values for each axis.
§Returns
Padded array of same type as input array
§Examples
use numrs2::prelude::*;
// Pad 1D array with constant value
let a = Array::from_vec(vec![1, 2, 3]);
let result = pad(&a, &[(2, 3)], "constant", Some(0)).expect("operation should succeed");
assert_eq!(result.to_vec(), vec![0, 0, 1, 2, 3, 0, 0, 0]);
// Pad 2D array with edge values
let b = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
let result = pad(&b, &[(1, 1), (2, 2)], "edge", None).expect("operation should succeed");
assert_eq!(result.shape(), vec![4, 6]);