extend
- mdtools.numpy_helper_functions.extend(a, length, axis=-1, fill_value=0)[source]
Extend an axis of an array to a given length by padding a given value at the end of the axis.
- Parameters:
a (
array_like
) – The array to extend.length (
int
) – The desired length of a.axis (
int
, optional) – The axis to extend. Default: last axisfill_value (
scalar
, optional) – The fill value to use to extend the given axis of a to the desired length. Note that you might have to change the data type of the input array to fit the type of fill_value before parsing the array to this function.
- Returns:
a_new (
numpy.ndarray
) – A copy of the input array with the given axis padded with fill_value in such a wax that it has the desired length. If the given axis already had the desired length or is even longer, just the input array is returned.
Examples
>>> a = np.arange(3) >>> mdt.nph.extend(a, 2) array([0, 1, 2]) >>> mdt.nph.extend(a, 5) array([0, 1, 2, 0, 0]) >>> mdt.nph.extend(a, 5, fill_value=9) array([0, 1, 2, 9, 9])
>>> a = np.arange(6).reshape(2, 3) >>> mdt.nph.extend(a, 5) array([[0, 1, 2, 0, 0], [3, 4, 5, 0, 0]]) >>> mdt.nph.extend(a, 4, axis=0) array([[0, 1, 2], [3, 4, 5], [0, 0, 0], [0, 0, 0]])
>>> a = np.arange(12).reshape(2, 2, 3) >>> mdt.nph.extend(a, 5) array([[[ 0, 1, 2, 0, 0], [ 3, 4, 5, 0, 0]], [[ 6, 7, 8, 0, 0], [ 9, 10, 11, 0, 0]]]) >>> mdt.nph.extend(a, 3, axis=0) array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]], [[ 0, 0, 0], [ 0, 0, 0]]]) >>> mdt.nph.extend(a, 3, axis=1) array([[[ 0, 1, 2], [ 3, 4, 5], [ 0, 0, 0]], [[ 6, 7, 8], [ 9, 10, 11], [ 0, 0, 0]]])