average.m

来自「Modeling and Forecasting Electricity Loa」· M 代码 · 共 36 行

M
36
字号
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 + =
减小字号Ctrl + -
显示快捷键?