weightedmedian.m (534B)
1 function medians=weightedmedian(X,weights); 2 3 % weightedmedian.m 11/19/99 4 % 5 % function medians=weightedmedian(X,weights); 6 % 7 % Function to compute the weighted median of the columns of a matrix. 8 % X = The matrix 9 % weights = The weights (need not sum to 1; we'll normalize). 10 11 N=size(X,2); 12 medians=zeros(1,N); 13 w=weights/sum(weights); 14 for i = 1:N; 15 [xs,indx]=sort(X(:,i)); 16 cumw=cumsum(w(indx)); 17 imore=find(cumw>=.5); 18 medians(i)=xs(imore(1)); % The first observation with cumweight>=.5 19 end; 20 21