MATLAB: Create New Column which is the same as another column but shifted by a row

columnduplicateMATLAB

Hi,
I am trying to create a new column in a table which has the values of another column, but from a different row.
i.e. if the variable 'Price' has row values of [ 1 ]
[ 2 ]
[ 3 ]
I want to create another column 'Price(t-1)' with values [ 1 ]
[ 1 ]
[ 2 ]
Note; the actualy case has varying integers which are not in order.
I've looked around but can't really find a place to start. I've attached an image of the 'Price' column in my table. Any help would be appreciated!

Best Answer

Something like the following code.
t = table([1;2;3;4;5], 'VariableNames', {'price'});
t
Result:
t =
5×1 table
price
_____
1
2
3
4
5
and then add the second column
t.price_1 = [t.price(1); t.price(1:end-1)];
t
Result:
t =
5×2 table
price price_1
_____ _______
1 1
2 1
3 2
4 3
5 4