相关分析在科研过程中常常会碰到,表征了一种两个因素间的相关程度,其值的大小只能说明相关性的强弱。
比如当我们有2000-2015年的NPP数据和2000-2015年的降水数据时,我们想查看两者在空间上随时间变化的
相关性。本文以产水和NPP为例进行说明
具体代码如下:
%authour@yinlichang3064@163.com
%将两者多年的数据放在三个不同的矩阵中
nppsum=zeros(3587*4642,16);
for year=2000:2015
filename=strcat('F:\课题项目\data\',int2str(year),'npp.tif');
data=importdata(filename);
data=reshape(data,3587*4642,1);
nppsum(:,year-1999)=data;
end
wcsum=zeros(3587*4642,16);
for year=2000:2015
filename=strcat('F:\课题项目\data\',int2str(year),'water_yield.tif');
data=importdata(filename);
data=reshape(data,3587*4642,1);
wcsum(:,year-1999)=data;
end
%相关性和显著性
npp_wc_xgx=zeros(3587,4642);
npp_wc_p=zeros(3587,4642);
for i=1:length(nppsum)
npp=nppsum(i,:);
if min(npp)>0 %注意这里的NPP的有效范围是大于0,如果自己的数据有效范围有小于0的话,则可以不用加这个
wc=wcsum(i,:);
[r2,p2]=corrcoef(npp,wc);
npp_wc_xgx(i)=r2(2);
npp_wc_p(i)=p2(2);
end
end
filename5='F:\result\npp_wc相关性.tif';
filename6='F:\npp_wc显著性.tif';
[a,R]=geotiffread('F:\项目\data\2002water_yield.tif');%先导入投影信息
info=geotiffinfo('F:\项目\data\2002water_yield.tif');
%%输出图像
geotiffwrite(filename5,npp_wc_xgx,R,'GeoKeyDirectoryTag',info.GeoTIFFTags.GeoKeyDirectoryTag);
geotiffwrite(filename6,npp_wc_p,R,'GeoKeyDirectoryTag',info.GeoTIFFTags.GeoKeyDirectoryTag);
如果自己的数据中存在负值,不想加限制条件的话,可以用下面的代码,但运行效率会降低,代码如下
更多需求,请查看个人介绍