MATLAB: Find max values of n previous values in a matrix without a loop.

for loopmaxrunning max

Hello,
I have a single column matrix and need to add a second column containing on each row the highest value of n previous row.
For example, my matrix would have this column:
Column1
1
4
3
6
3
5
4
7
2
5
6
1
I am looking to add a column containing the high value of 3 previous rows (including the current row) in the first column, so that I would obtain:
Column1 Column2
1 NaN
4 NaN
3 4
6 6
3 6
5 6
4 5
7 7
2 7
5 7
6 6
1 6
Is it possible in Matlab without a for to loop?
Many thanks,
Libor

Best Answer

a = randi(10,10,1);
your_result = max([a, circshift(a,1), circshift(a,2)],[],2);
your_result(1:2) = NaN;