# lag

## 说明

TT2 = lag(TT1)TT1 中每个变量的数据在时间上向前移动一个时间步。在时间上向前移动一次称为一个时滞TT1 的行时间必须为规则时间。

TT2 = lag(TT1,n) 将数据移动 n 个时间步。n 必须为整数。如果 n 为正，则 lag 在时间上将数据向前移动（一个时滞）。如果 n 为负，则 lag 在时间上将数据向后移动（一个时滞）。

TT2 = lag(TT1,dt) 按时间间隔 dt 移动数据。dt 为持续时间或日历持续时间。dt 必须为 TT1 的规则时间步的倍数。

## 示例

Time = datetime('2015-12-18 12:00:00') + days(1:5)';
Temp = [43 42.6 42.3 39.1 37.3]';
TT = timetable(Time,Temp)
TT=5×1 timetable
Time            Temp
____________________    ____

19-Dec-2015 12:00:00      43
20-Dec-2015 12:00:00    42.6
21-Dec-2015 12:00:00    42.3
22-Dec-2015 12:00:00    39.1
23-Dec-2015 12:00:00    37.3

TT2 = lag(TT)
TT2=5×1 timetable
Time            Temp
____________________    ____

19-Dec-2015 12:00:00     NaN
20-Dec-2015 12:00:00      43
21-Dec-2015 12:00:00    42.6
22-Dec-2015 12:00:00    42.3
23-Dec-2015 12:00:00    39.1

synchronize(TT,TT2)
ans=5×2 timetable
Time            Temp_TT    Temp_TT2
____________________    _______    ________

19-Dec-2015 12:00:00       43         NaN
20-Dec-2015 12:00:00     42.6          43
21-Dec-2015 12:00:00     42.3        42.6
22-Dec-2015 12:00:00     39.1        42.3
23-Dec-2015 12:00:00     37.3        39.1

Time = datetime('2015-12-18 12:00:00') + days(1:5)';
Temp = [43 42.6 42.3 39.1 37.3]';
TT = timetable(Time,Temp)
TT=5×1 timetable
Time            Temp
____________________    ____

19-Dec-2015 12:00:00      43
20-Dec-2015 12:00:00    42.6
21-Dec-2015 12:00:00    42.3
22-Dec-2015 12:00:00    39.1
23-Dec-2015 12:00:00    37.3

TT2 = lag(TT,2)
TT2=5×1 timetable
Time            Temp
____________________    ____

19-Dec-2015 12:00:00     NaN
20-Dec-2015 12:00:00     NaN
21-Dec-2015 12:00:00      43
22-Dec-2015 12:00:00    42.6
23-Dec-2015 12:00:00    42.3

TT2 = lag(TT,-3)
TT2=5×1 timetable
Time            Temp
____________________    ____

19-Dec-2015 12:00:00    39.1
20-Dec-2015 12:00:00    37.3
21-Dec-2015 12:00:00     NaN
22-Dec-2015 12:00:00     NaN
23-Dec-2015 12:00:00     NaN

Time = datetime('2015-12-01 12:00:00') + calmonths(1:5)';
Temp = [43 37 35 39 45]';
TT = timetable(Time,Temp)
TT=5×1 timetable
Time            Temp
____________________    ____

01-Jan-2016 12:00:00     43
01-Feb-2016 12:00:00     37
01-Mar-2016 12:00:00     35
01-Apr-2016 12:00:00     39
01-May-2016 12:00:00     45

TT2 = lag(TT,calmonths(2))
TT2=5×1 timetable
Time            Temp
____________________    ____

01-Jan-2016 12:00:00    NaN
01-Feb-2016 12:00:00    NaN
01-Mar-2016 12:00:00     43
01-Apr-2016 12:00:00     37
01-May-2016 12:00:00     35