-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakwavg.m
50 lines (36 loc) · 1.38 KB
/
makwavg.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function a = makwavg ( x , w , varargin )
%
% a = makwavg ( x , w )
% a = makwavg ( x , w , dim )
% a = makwavg ( __ , outtype )
% a = makwavg ( __ , nanflag )
%
% MET Analysis Kit. Computes the average of data x weighted by w. x and w
% must be numeric arrays with the same size. The weighted average is
% computed as:
%
% a = [ w( 1 )x( 1 ) + w( 2 )x( 2 ) + w( 3 )x( 3 ) + ... ] / sum( w )
%
% Optional input dim indicates along which dimension of x to compute the
% weighted average. outtype can be 'default', 'double', or 'native' while
% nanflag can be 'includenan' or 'omitnan'. See doc mean and doc sum for
% more information.
%
% Written by Jackson Smith - April 2018 - DPAG , University of Oxford
%
%%% Input check %%%
% Are x and w numeric?
if ~ isnumeric ( x ) || ~ isnumeric ( w )
error ( 'MAK:makwavg:xwnumeric' , ...
'makwavg: x and w must be numeric' )
% Are x and w the same size?
elseif numel ( x ) ~= numel ( w ) || ...
ndims ( x ) ~= ndims ( w ) || ...
any ( size( x ) ~= size( w ) )
error ( 'MAK:makwavg:xwsize' , ...
'makwavg: x and w must have the same size' )
end % check input
%%% Weighted average %%%
a = sum ( w .* x , varargin{ : } ) ./ ...
sum ( w , varargin{ : } ) ;
end % makwavg