accumarray
在 Matlab 中非常棒,我经常使用它。我有一个问题,我想传递给的函数accumarray
是加权平均值。即它接受两个向量,而不是一个向量。这似乎是不支持的用例accumarray
。
我的理解正确吗?
考虑,函数weightedAverage
function [ result ] = weightedMean( values, weights)
result = sum(values(:).*weights(:)) / sum(weights(:));
end
现在,我们要运行accumarray
如下:
subs = [1 1 1 2 2 3 3 3];
vals = [1 2 3 4 5 6 6 7];
weights = [3 2 1 9 1 9 9 9];
accumarray(subs, [vals;weights],[], @weightedMean)
但matlab返回:
Error using accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.