You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: cat_vol_iscale.m
    path of file: ./repos/ExploreASL/External/SPMmodified/toolbox/cat12
    
    the code of the file until where you have to start completion: function [TI,varargout] = cat_vol_iscale(T,action,vx_
    
function that may remove one hemissphere, but allows
      % a better WM peak (Tth) estimation.
      M2  = smooth3(TS>Tth*2/3 & TS<Tth*1.6 & G<Gth/4)>0.5; M2max=max(TS(M2(:)>0));
      Tth = peak(TS(M2>0)/M2max)*M2max*0.5 + 0.5*cat_stat_nanstat1d(TS(M2(:)),'median');
      M2  = cat_vol_morph(smooth3(TS>Tth*2/3 & TS<Tth*1.6 & G<Gth/3)>0.5,'l');
      TSD = cat_vol_localstat(T./Tth,M2,1,4); 
      n2  = cat_stat_nanstat1d(TSD(TSD>0),'mean');
      nc  = min(1.0,max(1/2,n2*10));
      TS  = cat_vol_smooth3X(single(T),nc); 
      clear TSD;
      % gradient map update after smoothing
      [gx,gy,gz] = cat_vol_gradient3(TS); G=abs(gx)+abs(gy)+abs(gz); 
      G=G./max(0,TS-mean(TS(:)/3)); G(isinf(G) | isnan(G) | G<0)=10; clear gx gy gz;

      % test functions
      % fprintf(' %0.2f %0.2f | %0.3f %0.3f',n1,n2,Gth,Tth); return
      % ds('l2','',vx_vol,TS/Tth,M2,G>Gth,TS/Tth,round(size(TS,3)*3/5)); pause(2); return
    
      %% background and distance estimation to find parts of brain tissue
    
      % background estimation
      [Hr,resTr] = cat_vol_resize(TS./G>0.5 & TS/Tth>0.2,'reduceV',vx_vol,8,8,'nearest');
      Hr = cat_vol_morph(Hr,'dc',3);
      H = cat_vol_resize(Hr,'dereduceV',resTr)>0.5; clear Hrr
      
      % distance estimation to find the brain 
      [Tr,Hr,resT] = cat_vol_resize({min(1.5,TS/Tth),H},'reduceV',vx_vol,redres,32,'mean');
      [Gr]         = cat_vol_resize(G,'reduceV',vx_vol,redres,32,'max');
      HDr = cat_vbdist(single(~Hr),true(size(Hr)),resT.vx_volr);
      [Mr,Dr] = cat_vol_downcut(single(~cat_vol_morph(Hr,'e')), ...
        1-Gr,0.1,resT.vx_volr,[double(eps('single')) 1]);
      Dr = Dr .* HDr/cat_stat_nanstat1d(HDr(:),'max'); %Dr = max(0,Dr - cat_stat_nanstat1d(Dr(:),'max')/2);
      Dr(isnan(Dr) | isinf(Dr))=0; clear Mr; 
      D  = cat_vol_resize(cat_vol_smooth3X(Dr),'dereduceV',resT);
  %  toc  
   
    
    
      %% BIAS Correction:
    tic
      TSO=TS; it=1; itmax=8; CV=0; CVO=inf; CVP=1; CVth=0.001; bs=3;
      while it<=itmax && CV<CVO-CVth && CV<CVP-CVth
       
        
      % Initial correction:
      % ----------------------------------------------------------------
      %tic
      if it>=1
        % GM/WM boundary estimation to classify the WM and GM segemnts:
        res1 = 2.5; %1.6;  
        [Gr,Dr,resT1] = cat_vol_resize({G,D},'reduceV',vx_vol,res1,32,'meanm');
        TSr  = cat_vol_resize(TS,'reduceV',vx_vol,res1,32,'max');
        M2r  = cat_vol_resize(M2,'reduceV',vx_vol,res1,32,'meanm')>0.5;
        TSXr = TSr .* cat_vol_morph(Gr<Gth*2 & TSr/Tth<1.5 & TSr/Tth>0.1,'lo'); TSXr=cat_vol_median3(TSXr,TSXr>0);
        
        % very rough maximum based correction for better GWM estimation
        [WIrr,resT2] = cat_vol_resize(TSXr,'reduceV',vx_vol,4,32,'max'); TSXrr=WIrr>0;
        WIrr = cat_vol_approx(WIrr,'nh',resT1.vx_volr,8); WIrr = cat_vol_smooth3X(WIrr,max(2,min(4,Tth/std(WIrr(TSXrr(:)))))); 
        WIr  = cat_vol_resize(WIrr,'dereduceV',resT2); clear WIrr; 
        WIr  = WIr * median(TSr(M2r(:) & Gr(:)<Gth & TSr(:)>WIr(:)*0.95) ./ WIr(M2r(:) & Gr(:)<Gth & TSr(:)>WIr(:)*0.95));
        
        %% estimating of the GM-WM threshold GWM
        GWMr = TSr .* (smooth3(Gr>Gth/2 & TSr./WIr<1.2 &  Gr<Gth*2 & TSXr>0)>0.5); 
        GWMr = TSr .* (GWMr>WIr*(0.6-2*(1 - mean(GWMr(GWMr(:)>0)./WIr(GWMr(:)>0))))) .* (GWMr<WIr*0.95);
        [GWMrr,resT2] = cat_vol_resize(GWMr,'reduceV',resT1.vx_volr,8,32,'meanm'); TSXrr=GWMrr>0;
        for i=1:4, GWMrr = cat_vol_localstat(GWMrr,GWMrr>0,2,1); end