function y = mean(x,dim) %MEAN Average or mean value. % For vectors, MEAN(X) is the mean value of the elements in X. For % matrices, MEAN(X) is a row vector containing the mean value of % each column. For N-D arrays, MEAN(X) is the mean value of the % elements along the first non-singleton dimension of X. % % MEAN(X,DIM) takes the mean along the dimension DIM of X. % % Example: If X = [0 1 2 % 3 4 5] % % then mean(X,1) is [1.5 2.5 3.5] and mean(X,2) is [1 if prod(size(x))==1, y = x; return, end if nargin==1, dim = min(find(size(x)~=1)); if isempty(dim), dim = 1; end end if ~isempty(x), y = sum(x,dim)/size(x,dim); else y = NaN; end