添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

trimdata

Trim data by removing elements

Since R2023b

collapse all in page

Description

example

B = trimdata( A , m ) trims A to size m by removing elements from the trailing side of A . For example, for a scalar size m :

  • If A is a vector, then trimdata(A,m) trims A to length m .

  • If A is a matrix, table, or timetable, then trimdata(A,m) trims A to have m rows.

  • If A is a multidimensional array, then trimdata(A,m) trims A to the size specified by m along the first dimension whose size is greater than 1.

If m is greater than or equal to the size of A in the operating dimension, then trimdata returns all of A .

example

B = trimdata( A , m , Name=Value ) specifies additional parameters for trimming using one or more name-value arguments. For example, trimdata(A,m,Side="leading") trims by removing elements from the beginning of A .

Examples

collapse all

Create a four-element column vector, and trim the vector to two elements. By default, the trimdata function removes elements from the trailing side of the vector.

A = [1; 3; 5; 7];
B = trimdata(A,2)
B = 2×1

Trim a row vector to two elements.

A2 = [2 4 6 8];
B2 = trimdata(A2,2)
B2 = 1×2
     2     4

Create two vectors with different lengths.

A1 = [2; 8; 3; 5];
A2 = [9; 4; 6; 2; 7; 7; 0];

Determine the length of the shorter vector.

szA1 = size(A1,1);
szA2 = size(A2,1);
[m,idx] = min([szA1 szA2])
m = 4
idx = 1

Trim the longer vector to match the length of the shorter vector.

B2 = trimdata(A2,m)
B2 = 4×1

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

C = [A1 B2]
C = 4×2
     2     9
     8     4
     3     6
     5     2

Create a 6-by-6 matrix. Trim the columns to a length of 3 by removing three elements from each column. Trim the rows to a length of 5 by removing one element from each row.

A = magic(6)
A = 6×6
    35     1     6    26    19    24
     3    32     7    21    23    25
    31     9     2    22    27    20
     8    28    33    17    10    15
    30     5    34    12    14    16
     4    36    29    13    18    11
B = trimdata(A,[3 5])
B = 3×5
    35     1     6    26    19
     3    32     7    21    23
    31     9     2    22    27

Create a matrix, and trim each matrix row to a length of 3. The trimmed data does not include the data in the fourth, fifth, and sixth columns of the input matrix.

A = magic(6)
A = 6×6
    35     1     6    26    19    24
     3    32     7    21    23    25
    31     9     2    22    27    20
     8    28    33    17    10    15
    30     5    34    12    14    16
     4    36    29    13    18    11
B = trimdata(A,3,Dimension=2)
B = 6×3
    35     1     6
     3    32     7
    31     9     2
     8    28    33
    30     5    34
     4    36    29

Create a vector in which the first few and last few elements are unneeded. Trim the vector by removing elements from the leading and trailing sides. Because the number of elements to trim is odd, trimdata 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 = trimdata(A,6,Side="both")
B = 1×6
     1     2     3     3     2     1

Input Arguments

collapse all

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

Note

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

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

  • If m is less than the size of A in the operating dimension, then trimdata removes elements.

  • If m is greater than or equal to the size of A in the operating dimension, then trimdata returns the input data without removing elements.

Name-Value Arguments

Specify optional pairs of arguments as Name1=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 = trimdata(A,m,Side="leading")

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:

  • If m is a scalar and A is an array, then the operating dimension is the first dimension whose size is greater than 1.

  • If m is a vector, then the operating dimensions are 1:numel(m) .

  • If A is a table or timetable, then the operating dimension is 1 or [1 2] , depending on the number of elements in m .

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

  • "trailing" — Trim the trailing elements of A .

  • "leading" — Trim the leading elements of A .

  • "both" — Trim A on both sides. If the number of elements to remove in the operating dimension is even, then trim the trailing and leading sides of A evenly. If the number of elements to remove in the operating dimension is odd, then trim the remaining element on the trailing side of A .

Tips

  • trimdata only removes elements from the input data. trimdata is recommended if you do not want to add additional data and you do not require the resized data to match the target size. If you require the resized data to respect the specified sizes in m , then consider using the resize function, which can also add elements to your data.

Extended Capabilities

Version History

Introduced in R2023b

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.