The answer in this link computes segmented averages as you describe except instead of averaging 1:8, 8:15, ... , it averages 1:8, 9:16, ..., . Solution
To replicate the edges, you just have to add three line.
The full solutions is shown in this demo.
data = 1:22;
winSz = 5;
replications = repmat([2;ones(winSz-2,1)],ceil(numel(data)/(winSz-1)),1);
dataPrep = repelem(data(:),replications(1:numel(data)));
dataPrep(1) = [];
if rem(numel(dataPrep),winSz)>0
nanAppend = nan(winSz - rem(numel(dataPrep),winSz),1);
else
nanAppend = [];
end
dataMat = reshape([dataPrep; nanAppend], winSz, []);
movingAverage = mean(dataMat,1,'Omitnan');
Results
The original data:
data =
Columns 1 through 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Columns 21 through 22
21 22
The blocked version where means are computed over each column
dataMat =
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 NaN
4 8 12 16 20 NaN
5 9 13 17 21 NaN
The means (within a 5-element window)
movingAverage =
3 7 11 15 19 21.5
Best Answer