MATLAB Accumarray weighted mean

前端 未结 4 1324
傲寒
傲寒 2020-12-17 02:06

So I am currently using \'accumarray\' to find the averages of a range of numbers wich correspond to matching ID\'s. Ex Input:

ID----Value
1     215
1     33         


        
4条回答
  •  一整个雨季
    2020-12-17 02:35

    Instead of using accumarray, you can directly compute a weighted mean, or many other functions, quite easily:

    nIDs = length(unique(ID));
    WeightedMean = zeros(nIDs, 1);
    
    for ii = 1:nIDs
        iID = (ID == ii);
        WeightedMean(ii) = (Value(iID)' * Weight(iID)) / sum(Weight(iID));
    end
    

    Is there a specific reason you wish to do this through accumarray?

提交回复
热议问题