My question is similar to this question about creating a lag variable. All I need to do is to implement this code in Matlab
.
For instance let's say we have the following two vectors in Matlab
:
ID =[2;2;2;2;3;3;5;5]
Pur=[0;1;2;3;1;2;4;5]
[ID,Pur]
ans =
2 0
2 1
2 2
2 3
3 1
3 2
5 4
5 5
I am looking for a way to lag the Purchase
variable for each ID. To get something like:
ans =
2 NA
2 0
2 1
2 2
3 NA
3 1
5 NA
5 4
Would appreciate your help.