function M = wmean(X,W,dim)
% WMEAN Weighted mean
%
% M = WMEAN(X,W)  Calculates the mean of X with each element
% weighted by W.
%
% M = WMEAN(X,W,dim) operates along dimension dim.

% Copyright (c) 2012
% Jerod Weinman
% jerod@acm.org

% $Id: wmean.m 51 2012-06-11 17:25:53Z weinman $
 
% Jerod Weinman
% jerod@acm.org
% /home/data/svn/vision/tools/package/GPL_STATEMENT
 



if nargin < 3 || isempty(dim)
  dim = find(size(X) ~= 1, 1);
  if isempty(dim), dim = 1; end
end

M = sum( bsxfun(@times,X,W) ,dim) ./ ...
    sum( bsxfun(@times,ones(size(X)), W), dim);
