weightedaverage.m (423B)
1 function avg=weightedaverage(x,weightvar,keepit); 2 3 % weightedaverage.m avg=weightedaverage(x,weightvar,keepit); 4 % 5 % x = NxK matrix to average (columnwise) 6 % weightvar = weighting variable, Nx1, e.g. population - need not sum to one (that's what keepit is for) 7 % keepit = logical Nx1 with "1" corresponding to observations to keep. 8 9 totweight=sum(weightvar(keepit)); 10 avg=sum(mult(x(keepit,:),weightvar(keepit)/totweight));