function zm=optiComputeMean(this,dataGroup,weights)

% OPTICOMPUTEMEAN - computes weighted average from optiStruct (target)
%
% Usage:
%  zm=optiComputeMean(optiStruct,dataGroup,weights(optional));
%

if nargin==2
    weights=this.weights;
end

zm=nansum(this.input(dataGroup).data.*repmat(weights,size(this.input(dataGroup).data,1),1),2);

function y = nansum(x,dim)
%NANSUM Sum, ignoring NaNs.
%   Y = NANSUM(X) returns the sum of X, treating NaNs as missing values.
%   For vector input, Y is the sum of the non-NaN elements in X.  For
%   matrix input, Y is a row vector containing the sum of non-NaN elements
%   in each column.  For N-D arrays, NANSUM operates along the first
%   non-singleton dimension.
%
%   Y = NANSUM(X,DIM) takes the sum along dimension DIM of X.
%
%   See also SUM, NANMEAN, NANVAR, NANSTD, NANMIN, NANMAX, NANMEDIAN.

%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision$  $Date$

% Find NaNs and set them to zero.  Then sum up non-NaNs.  Cols of all NaNs
% will return zero.
x(isnan(x)) = 0;
if nargin == 1 % let sum figure out which dimension to work along
    y = sum(x);
else           % work along the explicitly given dimension
    y = sum(x,dim);
end