One approach —
V = [3.4789; 0; 0; 3.3535; 0; 0; 3.2398; 3.2263; 0; 0; 0; 3.0846; 0; 0; 2.9815; 0; 2.9430; 0; 2.9055];
r0 = [2;3;5;6;9;10;11;13;14;16;18];
r1 = [1;4;7;8;12;15;17;19];
mvc = accumarray(r1, (1:numel(r1)).', [], @(x){mean(V([r1(x) r1(min([x+1,L]))]))});
Lv = cellfun(@(x)isempty(x), mvc, 'Unif',0);
mv = fillmissing(mv,'previous');
V([Lv{:}]) = mv([Lv{:}])
V =
3.4789
3.4162
3.4162
3.3535
3.2966
3.2966
3.2398
3.2263
3.1555
3.1555
Vbfr = buffer(V,10)
Vbfr =
3.4789 3.1555
3.4162 3.0846
3.4162 3.0331
3.3535 3.0331
3.2966 2.9815
3.2966 2.9623
3.2398 2.9430
3.2263 2.9242
3.1555 2.9055
3.1555 0
V = [3.4789; 0; 0; 3.3535; 0; 0; 3.2398; 3.2263; 0; 0; 0; 3.0846; 0; 0; 2.9815; 0; 2.9430; 0; 2.9055];
Comparison = table(V, Vm, 'VariableNames',{'Original','Filled'})
Comparison =
Original Filled
________ ______
3.4789 3.4789
0 3.4162
0 3.4162
3.3535 3.3535
0 3.2966
0 3.2966
3.2398 3.2398
3.2263 3.2263
0 3.1555
0 3.1555
0 3.1555
3.0846 3.0846
0 3.0331
0 3.0331
2.9815 2.9815
0 2.9623
First_10_Rows = Comparison(1:10,:)
First_10_Rows =
Original Filled
________ ______
3.4789 3.4789
0 3.4162
0 3.4162
3.3535 3.3535
0 3.2966
0 3.2966
3.2398 3.2398
3.2263 3.2263
0 3.1555
0 3.1555
Last_9_Rows = Comparison(11:end,:)
Last_9_Rows =
Original Filled
________ ______
0 3.1555
3.0846 3.0846
0 3.0331
0 3.0331
2.9815 2.9815
0 2.9623
2.943 2.943
0 2.9242
2.9055 2.9055
The accumarray call calculates the mean values, then computes logical vector ‘Lv’ to detect the empty cells that are then filled with NaN values. The ‘mvc’ cell array is then converted to a numerical array, and the NaN values are filled with the previous result using the fillmissing funciton. The ‘Lv’ vector us used again to fill the zeros in the original ‘V’ vector with the corresponding elements from ‘mv. to provide the result.
EDIT — (24 Feb 2024 at 15:46)
Added ‘Comparison’ table and sub-tables from it, extended the discussion of how the code works.
.