1
accumarray
在Matlab中是惊人的,我经常使用它。我有一个问题,我想传递给accumarray
的函数是加权平均值。即它采用两个向量,而不是单个向量。这似乎是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.
我不知道。你能举一个例子吗?也许你试过的代码? – beaker