function [newMean,newWeight,nonzeroChunkWeight,delta] = combineMean(oldMean,chunkMean,oldWeight,chunkWeight)
% Chan's algorithm for incremental update of mean

%   Copyright 2023 The MathWorks, Inc.

newMean = oldMean;
nonzeroChunkWeight = 0 < chunkWeight;
delta = chunkMean - oldMean;

newWeight = oldWeight + chunkWeight;

newMean(nonzeroChunkWeight,:) = oldMean(nonzeroChunkWeight,:) + (chunkWeight(nonzeroChunkWeight)./newWeight(nonzeroChunkWeight)).*delta(nonzeroChunkWeight,:);

end