Numpy填充数组
使用方法:
numpy.pad(array, pad_width, mode=’constant’, **kwargs)
array: 是待填充的数组
pad_width: 填充每一个轴填充的宽度
padwidth{sequence, arraylike, int}
Number of values padded to the edges of each axis. ((before_1, after_1), … (before_N, after_N)) unique pad widths for each axis. ((before, after),) yields same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes.
mode: 填充的模式,默认选’constant’
用法:
# 一维
a = [1, 2, 3, 4, 5]
np.pad(a, (2, 3), 'constant', constant_values=(4, 6))
-----------------------------------------------------------------------
# output:
array([4, 4, 1, ..., 6, 6, 6])
-----------------------------------------------------------------------
# 二维
a = [[1, 2], [3, 4]]
np.pad(a, ((3, 2), (2, 3)), 'minimum')
-----------------------------------------------------------------------
output:
array([[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1],
[3, 3, 3, 4, 3, 3, 3],
[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1]])
MATLAB填充三维数组
D = padarray(C,[3 3],0,'both')