Main Content

detrend

Subtract mean or best-fit line from timeseries object

Description

tsout = detrend(tsin,method) subtracts either a mean or a best-fit line from 2-D data in a timeseries using the specified method, and also removes all NaN values.

example

tsout = detrend(tsin,method,ind) specifies the indices of the columns or rows to detrend. ind is a vector of integers representing column indices for column-oriented data (tsin.IsTimeFirst is true) and representing row indices for row-oriented data (tsin.IsTimeFirst is false).

example

Examples

collapse all

Create a timeseries object with 2-D data, and subtract the mean of each column from the data.

A = magic(3)
A = 3×3

     8     1     6
     3     5     7
     4     9     2

tsin = timeseries(A,[1 2 3]);
tsout = detrend(tsin,'constant');
tsout.Data
ans = 3×3

     3    -4     1
    -2     0     2
    -1     4    -3

Subtract the mean of each column for only the second and third columns of data.

tscol = detrend(tsin,'constant',[2 3]);
tscol.Data
ans = 3×3

     8    -4     1
     3     0     2
     4     4    -3

Input Arguments

collapse all

Input timeseries containing 2-D data, specified as a scalar.

Data Types: timeseries

Detrend method, specified as one of the following options:

  • 'constant' — Subtract the mean from the data.

  • 'linear' — Subtract the best-fit line from the data.

Row or column indices, specified as a positive integer numeric scalar or vector. ind represents column indices for column-oriented data (tsin.IsTimeFirst is true) and represents row indices for row-oriented data (tsin.IsTimeFirst is false).

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Version History

Introduced before R2006a

See Also