%   Z-TransHarald.m

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.


global ResMat
ResMatZ=zeros(716,55);
ResMatZ(:,1:47)=ResMat(:,1:47);
IsNaNMat=~isnan(ResMat);
MedAro=median(ResMat(:,1));
                                                                      
LowAroInd=find(ResMat(:,1)<MedAro);
ResMatZ(:,54)=ones(716,1);
ResMatZ(LowAroInd,54)=-1.*ResMatZ(LowAroInd,54);
MedVal=median(ResMat(:,2));
NegValInd=find(ResMat(:,2)<MedVal);
ResMatZ(:,55)=ones(716,1);
ResMatZ(NegValInd,55)=-1.*ResMatZ(NegValInd,55);
for i=3:47
 	Tmp=find(IsNaNMat(:,i));
 	ResMatZ(Tmp,i)=(ResMat(Tmp,i)-mean(ResMat(Tmp,i)))/std(ResMat(Tmp,i));
end
v1=[3:3:47];
v2=[4:3:47];
v3=[5:3:47];
ResMatZ(:,49)=sum(IsNaNMat(:,v1),2);
ResMatZ(:,51)=sum(IsNaNMat(:,v2),2);
ResMatZ(:,53)=sum(IsNaNMat(:,v3),2);

MeanTmp=zeros(716,1);
for i=1:length(v1)
	Tmp=find(IsNaNMat(:,v1(i)));
	MeanTmp(Tmp)=MeanTmp(Tmp)+ResMatZ(Tmp,v1(i));
end
NotZeroTmp=find(ResMatZ(:,49));
ResMatZ(NotZeroTmp,48)=MeanTmp(NotZeroTmp)./ResMatZ(NotZeroTmp,49);

MeanTmp=zeros(716,1);
for i=1:length(v2)
	Tmp=find(IsNaNMat(:,v2(i)));
	MeanTmp(Tmp)=MeanTmp(Tmp)+ResMatZ(Tmp,v2(i));
end
NotZeroTmp=find(ResMatZ(:,51));
ResMatZ(NotZeroTmp,50)=MeanTmp(NotZeroTmp)./ResMatZ(NotZeroTmp,51);

MeanTmp=zeros(716,1);
for i=1:length(v3)
	Tmp=find(IsNaNMat(:,v3(i)));
	MeanTmp(Tmp)=MeanTmp(Tmp)+ResMatZ(Tmp,v3(i));
end
NotZeroTmp=find(ResMatZ(:,53));
ResMatZ(NotZeroTmp,52)=MeanTmp(NotZeroTmp)./ResMatZ(NotZeroTmp,53);

BasePath='Paul:Users:Harald:IAPS.app:';
SaveData(ResMatZ',[BasePath,'Base0-40T60-100.txt'],1,[],'ascii','ascii',[],[],[],1,1,4);
