resize

Changes the size of an array by cropping or padding.

Category: Language Fundamentals > Matrices and Arrays

Syntax

B = resize(A, sz)

Gives B the size sz, cropping or padding with zeros at the end of each dimension.

B = resize(A, len, 'Dimension', d)

Resizes only dimension d.

B = resize(___, Name, Value)

Options: 'FillValue', 'Pattern', 'Side'.

Description

When the target is larger than A the new elements are filled; when it is smaller, the extra elements are removed. It differs from reshape, which keeps all elements, and from paddata, which never shrinks. The original elements keep their positions when the side is 'trailing'.

Input arguments

A
Numeric array or cell array.
sz
Target size vector, or a single length together with 'Dimension'.
'FillValue'
Value for new elements (default 0).
'Pattern'
'constant' (default), 'edge', 'circular', 'flip' or 'reflect'.
'Side'
'trailing' (default), 'leading' or 'both': where to add or remove elements.

Output arguments

B
Array of exactly the requested size.

Example

resize([1 2; 3 4], [3 3])
resize([1 2 3 4], 2, 'Side', 'leading')
resize([1 2 3], 5, 'Pattern', 'circular')
Output
ans = 3×3

     1     2     0
     3     4     0
     0     0     0
ans = 1×2

     3     4
ans = 1×5

     1     2     3     1     2

See also

paddata reshape imresize