MATLAB: Storing data from looping and calculate again

calculate againdataloopmatrixstore

So i have this data
data =
2
3
1
4
5
6
the calculation that i need is find average from last 6 data and storing the result as last data, and keep repeating until certain data, in this case im trying until 5 new data. so from that data, my new data should be
newdata=
2
3
1
4
5
6
3.5
3.75
3.875
4.35
4.41
im still stuck until this code
clc
data=[2;3;1;4;5;6]
for i=1:5
newdata(i)=mean(data);
end
newdata'

Best Answer

Very simple way would be like this. If you want to apply this for much larger numerical array, then vectorization technique will be needed.
data=[2;3;1;4;5;6];
newdata = zeros(11,1);
newdata(1:6) = data;
for kk = 7:11
newdata(kk) = mean(newdata(kk-6:kk-1));
end