I would go as follows:
Change the vector so that it is a 3 Γ x matrix:
x=[1:12]; xx=reshape(x,3,[]); % xx is now [1 4 7 10; 2 5 8 11; 3 6 9 12]
after that
yy = sum(xx,1)./size(xx,1)
and now
y = reshape(repmat(yy, size(xx,1),1),1,[])
produces exactly your desired result.
Your parameter 3 , indicating the number of values, is used in only one place and can be easily changed if necessary.
source share