📄 average.m
字号:
function y=average(n,w,glue)
%AVERAGE Weighted average.
% Y=AVERAGE(N,W) returns the weighted average of the vector N
% with weights given by the vector W.
% Y=AVERAGE(N,W,1) returns the weighted average of the vector N
% with weights given by the vector W, where the average for the first
% observations is calculated using a few last obs. and vice versa.
% Written by Rafal Weron (1997.02.12, rev. 2001.02.02)
% Copyright (c) 1997-2006 by Rafal Weron
if nargin<3,
glue = 0;
end;
n = n(:);
nn = length(n);
w = w(:);
ww = length(w);
k = floor(ww/2);
if ww/2 == k,
error('Weights vector W must have odd length.');
end;
if glue == 1,
n = [n(end-k+1:end)' n' n(1:k)']';
else
n = [ones(k,1)'*n(1) n' ones(k,1)'*n(nn)]';
end;
y = zeros(nn,1);
s = sum(w);
for i=1:nn,
y(i) = sum(w.*n(i:i+ww-1))/s;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -