0001 function arrMean = slarrmean(data, arrsiz, n, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 if nargin < 3
0036 raise_lackinput('slarrmean', 3);
0037 end
0038
0039 if isnumeric(data)
0040 isdirect = true;
0041 arrs = data;
0042 arrsiz = arrsiz(:)';
0043 if ~isequal(size(arrs), [arrsiz, n])
0044 error('sltoolbox:sizmismatch', ...
0045 'The size of arrs (data) is invalid');
0046 end
0047
0048 elseif iscell(data)
0049 isdirect = false;
0050 fns = data;
0051 arrsiz = arrsiz(:)';
0052 nfiles = numel(fns);
0053
0054 else
0055 error('sltoolbox:invalidarg', ...
0056 'The first argument for slarrmean should be an numeric array or a cell array of file names');
0057 end
0058
0059 opts.weights = [];
0060
0061 opts = slparseprops(opts, varargin{:});
0062 hasweights = ~isempty(opts.weights);
0063
0064 if (hasweights)
0065 opts.weights = opts.weights(:);
0066 if length(opts.weights) ~= n
0067 error('sltoolbox:invalidarg', ...
0068 'The length of weights is inconsistent with the number of units');
0069 end
0070 end
0071
0072
0073
0074
0075 if isdirect
0076 arrMean = compute_array_sum(arrs, arrsiz, n, opts.weights);
0077 else
0078 arrMean = zeros(arrsiz);
0079 c = 0;
0080 for i = 1 : nfiles
0081 curarrs = slreadarray(fns{i});
0082 curn = size(curarrs, length(arrsiz) + 1);
0083 if ~hasweights
0084 arrMean = arrMean + compute_array_sum(curarrs, arrsiz, curn, []);
0085 else
0086 arrMean = arrMean + compute_array_sum(curarrs, arrsiz, curn, opts.weights(c+1:c+curn));
0087 end
0088 c = c + curn;
0089
0090 if c > n
0091 error('sltoolbox:sizmismatch', ...
0092 'The total number of units in the set of array files is not n');
0093 end
0094 end
0095
0096 if c ~= n
0097 error('sltoolbox:sizmismatch', ...
0098 'The total number of units in the set of array files is not n');
0099 end
0100
0101 end
0102
0103 if ~hasweights
0104 arrMean = arrMean / n;
0105 else
0106 arrMean = arrMean / sum(opts.weights);
0107 end
0108
0109
0110
0111
0112
0113 function S = compute_array_sum(arrs, arrsiz, n, w)
0114
0115 if ~isequal(size(arrs), [arrsiz, n])
0116 error('sltoolbox:sizmismatch', ...
0117 'The size of array is not consistent as specified');
0118 end
0119
0120 d = length(arrsiz);
0121 if isempty(w)
0122 S = sum(arrs, d+1);
0123 else
0124 S = reshape(arrs, [prod(arrsiz), n]) * w;
0125 if d == 1
0126 S = reshape(S, [arrsiz, 1]);
0127 else
0128 S = reshape(S, arrsiz);
0129 end
0130 end
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140