“基础统计指标Matlab编码计算”

一般水平指标

function value = average(X)[row,~]=size(X);sum=0;for i=1:row sum= sum+X(i,1);endvalue=sum/row;%直接利用matlab自带函数mean也可求解平均值%value=mean(X)end中位数

function value = median1(X)[row,~]=size(X);X1 = sort(X,1,'ascend'); %升序排列if mod(row,2)==0 %样本数量为偶数时 value=(X1(row/2,1)+X1(row/2+1,1))/2;else %样本数量为奇数时 value=X1((row+1)/2,1);end%直接利用matlab自带函数median也可求解中位数%value=median(X)end众数

function value = mode1(X) X1 = unique(X);%求解X中不重复元素 X2 = histc(X(:),X1);%求解不重复元素出现的次数 [~,I] = max(X2);%求解次数最大值及其对应的索引 value = X1(I,1); %直接利用matlab自带函数mode也可求解众数 %value=mode(X)end

离散程度指标

function value = range1(X) value=max(X)-min(X); %直接利用matlab自带函数range也可求解极差 %value=range(X)end离差

function value = deviation(X)[row,~]=size(X);value=zeros(row,1);for i=1:row value(i,1)=X(i,1)-mean(X); %离差endend离差平方和

function value = SSD(X) %sum of squares of deviations[row,~]=size(X);for i=1:row dev(i,1)=X(i,1)-mean(X);endvalue=0;for i=1:row value = value + dev(i,1)^2; %离差平方和endend方差

function value = variance(X)[row,~]=size(X);for i=1:row dev(i,1)=X(i,1)-mean(X);endvalue=0;for i=1:row value = value + dev(i,1)^2; %离差平方和endvalue = value/row; %方差end标准差

function value = std1(X)[row,~]=size(X);for i=1:row dev(i,1)=X(i,1)-mean(X);endvalue=0;for i=1:row value = value + dev(i,1)^2; %离差平方和endvalue = value/row; %方差value = sqrt(value); %标准差end无偏估计

function value = std2(X)[row,~]=size(X);for i=1:row dev(i,1)=X(i,1)-mean(X);endvalue=0;for i=1:row value = value + dev(i,1)^2; %离差平方和endvalue = sqrt(value/(row-1)); %标准差无偏估计end变异系数

function value = Cv(X) %coefficient of variation[row,~]=size(X);for i=1:row dev(i,1)=X(i,1)-mean(X);endvalue=0;for i=1:row value = value + dev(i,1)^2; %离差平方和endvalue = sqrt(value/(row-1)); %标准差无偏估计value = value/mean(X); %变异系数end

分布特征指标

偏度系数

function value = skewness1(X)[row,~]=size(X);value=zeros(row,1);for i=1:row dev(i,1)=X(i,1)-mean(X); %离差endssd=0;for i=1:row ssd = ssd + dev(i,1)^2; %离差平方和endstd = sqrt(ssd/row); %标准差value=0;for i=1:row value = value + (dev(i,1)/std)^3;endvalue = value/row; %偏度系数%直接利用matlab自带函数skewness也可求解偏度系数%value=skewness(X)end峰度系数

function value = kurtosis1(X)[row,~]=size(X);value=zeros(row,1);for i=1:row dev(i,1)=X(i,1)-mean(X); %离差endssd=0;for i=1:row ssd = ssd + dev(i,1)^2; %离差平方和endstd = sqrt(ssd/row); %标准差value=0;for i=1:row value = value + (dev(i,1)/std)^4;endvalue = value/row -3; %峰度系数%直接利用matlab自带函数kurtosis也可求解峰度系数%value=kurtosis(X)%注意:matlab自带的kurtosis函数计算公式中,没有减去3这个值!end

高程采样数据.csv导入至Matlab存为列向量waterflow,调用各基础指标计算函数进行计算:

%高程采样数据.csvfprintf('平均值 = %f\n',mean(waterflow))%平均值fprintf('中位数 = %f\n',median1(waterflow))%中位数fprintf('众数 = %f\n',mode1(waterflow))%众数fprintf('极差 = %f\n',range1(waterflow))%极差fprintf('离差平方和 = %f\n',SSD(waterflow))%离差平方和fprintf('方差 = %f\n',variance(waterflow))%方差fprintf('标准差 = %f\n',std1(waterflow))%标准差fprintf('无偏估计 = %f\n',std2(waterflow))%标准差无偏估计fprintf('变异系数 = %f\n',Cv(waterflow))%变异系数fprintf('偏度系数 = %f\n',skewness1(waterflow))%偏度系数fprintf('峰度系数 = %f\n',kurtosis1(waterflow))%峰度系数fprintf('样本离差 = \n')%样本离差deviation(waterflow)

计算结果如下:

平均值 = 7031.中位数 = 5440.000000众数 = 10900.000000极差 = 22950.000000离差平方和 = 9100226613.698628方差 = 24932127.708763标准差 = 4993.208158无偏估计 = 5000.062256变异系数 = 0.711080偏度系数 = 1.113147峰度系数 = 0.751043样本离差 = ans = 1.0e+04 * -0.5712 -0.5812 -0.5902 -0.5982 -0.5932 -0.5832 -0.5722 -0.5612 -0.5512   ......   ...