resize - Resize data by adding or removing elements - MATLAB (original) (raw)

Resize data by adding or removing elements

Since R2023b

Syntax

Description

`B` = resize([A](#mw%5F39010242-dc42-4436-9920-d0ad8f891dd9),[m](#mw%5F5192f5c9-11f8-4941-997a-5f9dc2ebf8d6)) resizes A to size m by adding elements to or removing elements from the trailing side of A. For example, ifm is a scalar:

If m is a vector, then each element in m represents the size of the corresponding dimension in B.

example

`B` = resize([A](#mw%5F39010242-dc42-4436-9920-d0ad8f891dd9),[m](#mw%5F5192f5c9-11f8-4941-997a-5f9dc2ebf8d6),[Name=Value](#namevaluepairarguments)) specifies additional parameters for resizing using one or more name-value arguments. For example, resize(A,m,Pattern="circular"), where m is greater than the size of A in the operating dimension, resizes by repeating the input data circularly.

example

Examples

collapse all

Resize Vector

Create a four-element column vector, and resize the vector to six elements. By default, the resize function adds zeros to the trailing side of the numeric vector.

A = [1; 3; 5; 7]; B1 = resize(A,6)

Resize the vector to two elements. By default, resize removes elements from the trailing side.

Match Length of Another Vector

Create three vectors with different lengths.

A1 = [2; 8; 3]; A2 = [9; 4; 6; 2; 7]; A3 = [9; 2; 6; 1; 9; 3];

Determine the length of the second vector.

Resize the first and third vectors to match the length of the second vector. resize adds elements to the end of A1 and removes an element from the end of A3.

You can concatenate vectors of the same length. Create a matrix using the resized vectors.

C = 5×3

 2     9     9
 8     4     2
 3     6     6
 0     2     1
 0     7     9

You can also resize data using the paddata and trimdata functions. Use the paddata function to resize the vector shorter than the target length, and use the trimdata function to resize the vector longer than the target length.

Resize Matrix

Create a 3-by-3 matrix. Resize the columns to a length of 2 by removing one element from each column. Resize the rows to a length of 9 by reflecting the data in each row until the row contains nine elements. Pattern applies only to the dimension for which resize adds elements.

A = [1 3 5; 2 4 6; 7 8 10]

A = 3×3

 1     3     5
 2     4     6
 7     8    10

B = resize(A,[2 9],Pattern="reflect")

B = 2×9

 1     3     5     3     1     3     5     3     1
 2     4     6     4     2     4     6     4     2

Add Array Page

Create a 3-by-3 matrix as the first page in a 3-D array. Add a second page to the array by resizing along the third dimension.

A = [1 3 5; 2 4 6; 7 8 10]

A = 3×3

 1     3     5
 2     4     6
 7     8    10

B = resize(A,2,Dimension=3)

B = B(:,:,1) =

 1     3     5
 2     4     6
 7     8    10

B(:,:,2) =

 0     0     0
 0     0     0
 0     0     0

Specify Fill Values for Table Variables

Create a timetable with variables of different data types.

num = [10; 20; 30]; cat = categorical(["A"; "B"; "A"]); log = logical([1; 0; 1]); str = ["Text 1"; "Text 2"; "Text 3"]; TT = timetable(num,cat,log,str,Timestep=hours(2))

TT=3×4 timetable Time num cat log str
____ ___ ___ _____ ________

0 hr    10      A     true     "Text 1"
2 hr    20      B     false    "Text 2"
4 hr    30      A     true     "Text 3"

Resize each timetable variable to six elements. resize uses the default fill value for the data type of each variable. The default fill value for each data type is the same as the value of elements that MATLAB® creates when assigning a value beyond the last row of the table.

B1=6×4 timetable Time num cat log str
_____ ___ ___________ _____ _________

0 hr     10     A              true     "Text 1" 
2 hr     20     B              false    "Text 2" 
4 hr     30     A              true     "Text 3" 
6 hr      0     <undefined>    false    <missing>
8 hr      0     <undefined>    false    <missing>
10 hr     0     <undefined>    false    <missing>

Specify a custom fill value for the elements to add to each timetable variable. resize extends the row times, so you do not need to specify a fill value for the row times.

B2 = resize(TT,6,FillValue={mean(num),"C",1,""})

B2=6×4 timetable Time num cat log str
_____ ___ ___ _____ ________

0 hr     10      A     true     "Text 1"
2 hr     20      B     false    "Text 2"
4 hr     30      A     true     "Text 3"
6 hr     20      C     true     ""      
8 hr     20      C     true     ""      
10 hr    20      C     true     ""      

Trim Both Sides of Vector

Create a vector in which the first few and last few elements are unneeded. Resize the vector by removing elements from the leading and trailing sides. Because the number of elements to remove is odd, resize removes one more element from the trailing side of the vector than the leading side.

A = [0.1 1 2 3 3 2 1 0 NaN]; B = resize(A,6,Side="both")

Input Arguments

collapse all

A — Input data

vector | matrix | multidimensional array | table | timetable | cell array | structure array

Input data, specified as a vector, matrix, multidimensional array, table, timetable, cell array, or structure array.

Note

If A is a cell array, then resize changes the size of the entire array. It does not change the size of each cell in the array. Use the cellfun function to applyresize to each cell in a cell array.

m — Size of resized data along operating dimension

nonnegative integer scalar | vector of nonnegative integers

Size of resized data along operating dimension, specified as a nonnegative integer scalar or vector of nonnegative integers. Each element represents the size of the resized data in an operating dimension.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: B = resize(A,m,Pattern="circular")

Dimension — Dimensions to operate along

"auto" (default) | positive integer scalar | vector of positive integers

Dimensions to operate along, specified as "auto", a positive integer scalar, or a vector of positive integers. Each element represents a dimension of the input data.

If Dimension is "auto", then the operating dimension depends on the input arguments:

FillValue — Fill value

[] (default) | scalar | cell array | scalar structure

Fill value for added elements, specified as a scalar, cell array, or scalar structure.

The default fill value for each class is the same as the value of elements that MATLAB® creates when assigning a value past the end of a vector. For example, the default fill value for numeric input data is 0.

If you specify FillValue, you cannot specifyPattern.

Pattern — Pattern for adding elements

"constant" (default) | "edge" | "circular" | "flip" | "reflect"

Pattern for adding elements, specified as one of the pattern names in the table. The pattern is repeated until the resized data has size m.

If you specify Pattern, you cannot specifyFillValue.

This table lists the pattern names with a description and a sample of how each pattern resizes the input data A = [1 2 3].

Pattern Name Description Resized Data
"constant" Pad data with the default value determined by the data type ofA. Sample of padding data using the "constant" pattern
"edge" Pad data by replicating the leading and trailing endpoints as constant fill values. Sample of padding data using the "edge" pattern
"circular" Pad data by repeating the input data circularly. Sample of padding data using the "circular" pattern
"flip" Pad data by flipping the input data symmetrically. Endpoints are duplicated. Sample of padding data using the "clip" pattern
"reflect" Pad data by reflecting the input data. Endpoints are not duplicated. Sample of padding data using the "reflect" pattern

Side — Side of input data for resizing

"trailing" (default) | "leading" | "both"

Side of input data for resizing, specified as one of these values:

Tips

Extended Capabilities

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™. (since R2024b)

Usage notes and limitations:

GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. (since R2024a)

The resize function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. (since R2024a)

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced in R2023b