您可以通过加权平均使用diff
和的组合来包含“缺失”条目:sum
% time step
step = 1;
% Example data (with repeated elements)
A = [...
1 80.6
2 79.8
3 40.3
4 40.3
5 81.9
6 83.6
7 83.7
8 95.4
9 14.8
10 14.8
11 14.8
12 14.8
13 14.8
14 44.3];
% Example data, with the repeated elements removed
B = [...
1 80.6
2 79.8
3 40.3
5 81.9
6 83.6
7 83.7
8 95.4
9 14.8
14 44.3];
% The correct value
M = mean(A(:,2))
% The correct value for the pruned data
D = diff(B(:,1));
W = [D/step; 1];
M = sum( W .* B(:,2))/sum(W)
结果:
M1 =
5.027857142857141e+001
M2 =
5.027857142857143e+001
或者,您可以通过运行长度编码A
从缩写重新创建完整向量。B
您可以像这样有效地做到这一点:
W = [diff(B(:,1))/step; 1];
idx([cumsum([true; W(W>0)])]) = true;
A_new = [ (B(1,1):step:B(end,1)).' B(cumsum(idx(1:find(idx,1,'last')-1)),2) ];