function deltaX = standDev(x,w)
% Returns the standard deviation of a set of values x and weights w.
% Input
%   x : vector of values x_i
%   w : optional vector of weights, with same lenght as x
%       if not present, all weights are assumed equal 
% Output
%   deltaX = sqrt( (sum_i (x_i - meanX)^2 * w_i) / (sum_i w_i) )
%   where meanX = (sum_i x_i * w_i) / (sum_i w_i)
% Usage
%    deltaX = standDev(x,w)
% Author
%   Jose M Soler. Nov.2010

if nargin<2             % standDev was called without argument w
  w=ones(size(x));
end
p = w/sum(w);           % probability of each x
meanX = sum(x.*p);
deltaX = sqrt( sum((x-meanX).^2 .* p) );
end

