?? geomean2.m
字號:
function [gm]=geomean2(y, dim)
% % geomean2: Calculates the geometric mean
% %
% % Syntax:
% %
% % [gm]=geomean2(y);
% %
% % ********************************************************************
% %
% % Description
% %
% % This program calculates the geometric mean, if all of the data are
% % greater than zero; otherwise, the progrma caculates the
% % arithmetic mean.
% %
% %
% % ********************************************************************
%
% Example='1';
%
% y=rand(1, 100000); % y is the data array
% % y can be multidimensional
%
% [gm]=geomean2(y);
% %
% % Output Variables
% % gm is the mean
% %
% %
% %
% % *********************************************************************
% %
% % Program Written by Edward L. Zechmann
% %
% % date 16 December 2007
% %
% % modified 27 December 2007 Added a description and an example
% % Updated comments.
% %
% % modified 7 January 2009 Updated comments.
% %
% % *********************************************************************
% %
% % Please feel free to modify this code.
% %
flag2=0;
if nargin < 1 || isempty(y) || ~isnumeric(y)
y=[];
gm=[];
flag2=1;
end
if isequal(flag2, 0)
flag1=0;
if nargin < 2 || isempty(dim) || ~isnumeric(dim)
dim=[];
flag1=1;
end
dim=round(dim);
if isequal(flag1, 1)
boolean1=any(min(y <= 0));
if boolean1
gm=mean(y, []);
else
bb=prod(y);
gm=bb.^(1./(numel(y)/numel(bb)));
end
else
boolean1=any(min(y <= 0, dim));
if dim > ndims(y)
gm=y;
else
if boolean1
gm=mean(y, [], dim);
else
gm=prod(y, dim).^(1./size(y, dim));
end
end
end
end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -