以下是一个简单的 Mann-Kendall 程序的 MATLAB 实现:
function [p,h,S,T,z]=mannkendall(x)
% Mann-Kendall testfor trend
% x - time series data
% p - p-value
% h - hypothesis test result, h=0 no trend, h=1 trend exists
% S - test statistic
% T - number of positive slopes
% z - Z-transform of the test statistic
n=length(x);
x=reshape(x,n,1);
% Calculate the S statistic
S=0;
for i=1:n-1
for j=i+1:n
S=S+sign(x(j)-x(i));
end
end
% Calculate the variances and Z-transform
var_S=(n*(n-1)*(2*n+5))/18;
z=(S-1)/sqrt(var_S);
% Calculate the p-value
p=2*(1-normcdf(abs(z)));
% Hypothesis test
if p<0.05
h=1;
else
h=0;
end
% Calculate T
T=S/2;