简体   繁体   中英

RMSE in 3D coordinates (MATLAB)

I have a 3D data (of climate variables), (x=33,y=35,z=27) where x,y refer to the grid points and z is time(in yrs). I need grid wise RMSE for the given time period. I've to mask the file before calculating RMSE. The dimensions of mask file and data are same (including time). The no. of grids for which RMSE has to be found is reduced after masking(33*35=1155 to 357). I found RMSE for these reduced grids after reshaping the original data. Now I have a column matrix of RMSE (357*1) which has to be shown in 2d ie for the respective grids (33*35). How do perform reshaping from a 1d smaller array to 2d bigger array? My code is:

IMD_load=load('IMD.mat'); % mask file cum observation data
IMD1=IMD_load.IMD_OBS;
IMD(:,:,:)=IMD1(:,:,1:27); % I'm doing only for 27 yrs of available time period

CFS_load=load('CFS.mat'); % model data
CFS1=CFS_load.CFSV2;
CFS2=CFS1(:,:,1:27); % I'm doing only for 27 yrs of available time period
CFS=CFS2-273.15;

for i = 1:1:27
  IMD2=IMD(:,:,i);
  IMD_1=reshape(IMD2,1155,1);
  IMD_1(isnan(IMD_1))=0;
  [IMD_m]=find(IMD_1==0);   

%  CFS3=CFS(:,:,i);
  CFS4=reshape(CFS(:,:,i),1155,1);
  CFS4(IMD_m,:)=0;
  CFS5=reshape(CFS4,1155,1);
  CFS6(:,i)=CFS5;
end

% I have to run the above for loop because for each year no. of grids with NaN is varying

OO1=reshape(IMD,1155,27);
MM1=reshape(CFS,1155,27);

OO2=reshape(OO1,1155*27,1); % is this corret?
MM2=reshape(MM1,1155*27,1); % is this corret?

[cfs6_m]=find(CFS6~=0);
[cfs6_m2]=find(CFS6==0);
OO3=reshape(OO2(cfs6_m,:),357,27); % (OR) OO4=reshape(OO2(cfs6_m),357,27);
MM3=reshape(MM2(cfs6_m,:),357,27); % (OR) MM4=reshape(MM2(cfs6_m),357,27);

for i = 1:1:357
  r2(i,:)=sqrt(sum((OO3(i,:)-MM3(i,:)).^2)/27);
end
% RR(1155,1)=reshape(R,33,35);

% I'm now left with 'r1'(357*1). But how do I convert it back to 33*35???

Does the following not work?

temp=reshape(R,33,35);
RR(1155,1)=temp;

My friend suggested me the conventional method of using three for-loops. For now my problem is solved. But you may suggest more efficient and compact code. My code is given below. If you notice something wrong with it, please tell me.

IMD_load=load('IMD.mat');
IMD1=IMD_load.IMD_OBS;
IMD(:,:,:)=IMD1(:,:,1:27);

CFS_load=load('CFSV2.mat');
CFS1=CFS_load.CFSV2;
CFS2=CFS1(:,:,1:27);
CFS=CFS2-273.15;

for i = 1:1:27
  IMD2=IMD(:,:,i);
  IMD_1=reshape(IMD2,1155,1);
  IMD_1(isnan(IMD_1))=0;
  [IMD_m]=find(IMD_1==0);

%   CFS3=CFS(:,:,i);
  CFS4=reshape(CFS(:,:,i),1155,1);
  CFS4(IMD_m,:)=0;
  CFS5=reshape(CFS4,1155,1);
  CFS6(:,:,i)=reshape(CFS5,33,35);
end

rms(33,35,27)=0;
for i=1:1:27
  for j=1:1:35
    for k=1:1:33
        if (CFS6(k,j,i)~=0) || (IMD(k,j,i)~=0)
            rms(k,j,i)=((IMD(k,j,i)-CFS6(k,j,i)).^2);
        end
    end
  end
end

rms1(33,35)=0;
for i=1:1:27
  rms1(:,:)=rms(:,:,i)+rms1(:,:);
end

rms2=rms1/27;
TMAX_rmse=sqrt(rms2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM