accumarray
in Matlab is amazing, and I use it often. I have a problem where the function I would like to pass to accumarray
is a weighted average. i.e. it takes in two vectors, not a single vector. This appears to be a use case not supported by accumarray
.
Is my understanding correct?
Consider, the function weightedAverage
function [ result ] = weightedMean( values, weights)
result = sum(values(:).*weights(:)) / sum(weights(:));
end
Now, we want to run accumarray
as follows:
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)
but matlab returns:
Error using accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.